View rubocop.rb
def if_statement
return a unless condition?
c(
1,
2,
3
)
end
View rubocop.rb
def if_statement
a
if condition?
c(
1,
2,
3
)
end
end
View test.rb
class Parent < ActiveRecord::Base
has_many :children
#Question: Does method below perform a join in children and match only children belonging to a specific parent?
def has_active_children
children.exists?(status: "active")
end
View Routine wanted as Back Job
# Fetchs campaigns from SiB
class CampaignFetch
def get
def sib
@sib ||= Mailin.new("API_URL","API_KEY")
end
def shoot_criteria
{ "type"=>"classic", "status" => "sent", "page"=>10,"page_limit"=>1000 }
View Controller
# fetchs SIB Account information
def sib_account_fetch
sib = Mailin.new("API_URL","API_KEY")
acct = sib.get_account()
acct.dig('data')
credits = acct.dig('data', 0).to_h
SibAccount.find(1).update_attributes(credits)
details = acct.dig('data', 2).to_h
SibAccount.find(1).update_attributes(details).delay
View Nokogiri scraper
require 'rubygems'
require 'nokogiri'
require 'open-uri'
page = Nokogiri::HTML(open("http://v-tac.eu/led-lights/lights1/led-spotlights/led-spotlight-7w-gu10-smd-white-plastic-white-detail.html"))
puts page.class # => Nokogiri::HTML::Document
fields = page.css(".product-field-type-S")
fields.each do |field|
key = field.css(".product-fields-title").text
View models.rb
class User < ActiveRecord::Base
has_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
View where_preference.rb
class User < ActiveRecord::Base
def self.my_where(key, value)
# Normally with 2 arguments, you can easily do:
where(key: value)
end
def self.where_preference(conditions)
# But with a map and several k-v pairs, you'd have to do something like
conditions.inject(self) do |scope, (k, v)|
scope.where(k => v)
View Gemfile
source 'https://rubygems.org'
gem "pry"
gem 'i18n', github: "svenfuchs/i18n", branch: "173-reset-load-path"
gem "rspec"
View transactions.ex
defmodule Transaction do
defstruct [:merchant_id, :financial_context_id]
end
defmodule Transactions do
def process do
transactions = [
%Transaction{merchant_id: "12345", financial_context_id: "1234"},
%Transaction{merchant_id: "12345", financial_context_id: "1234"},
%Transaction{merchant_id: "12345", financial_context_id: "1234"},