Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_compatable/active_record/relation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_with_args(*args) ⇒ Object



47
48
49
50
# File 'lib/rails_compatable/active_record/relation.rb', line 47

def all_with_args(*args)
  return all_without_args if args.blank?
  scoped(*args)
end

.method_missing(*args) ⇒ Object

Raises:

  • (NoMethodError)


41
42
43
44
45
# File 'lib/rails_compatable/active_record/relation.rb', line 41

def method_missing(*args)
  matched, new_method, fields_joined_by_end = args.first.to_s.match(/(find_by|find_or_initialize_by)_(\w+)/).to_a
  return send(new_method, [fields_joined_by_end.split('_and_'), args[1..-1]].transpose.to_h) if matched
  raise NoMethodError, "undefined method '#{args.first}'"
end

Instance Method Details

#conditions(*args) ⇒ Object



37
38
39
# File 'lib/rails_compatable/active_record/relation.rb', line 37

def conditions(*args)
  where(*args)
end

#find_by(*args) ⇒ Object



4
5
6
# File 'lib/rails_compatable/active_record/relation.rb', line 4

def find_by(*args)
  where(*args).take
end

#find_or_initialize_by(attributes, &block) ⇒ Object



20
21
22
# File 'lib/rails_compatable/active_record/relation.rb', line 20

def find_or_initialize_by(attributes, &block)
  find_by(attributes) || new(attributes, &block)
end

#find_takeObject



12
13
14
15
16
17
18
# File 'lib/rails_compatable/active_record/relation.rb', line 12

def find_take
  if loaded?
    @records.first
  else
    @take ||= limit(1).to_a.first
  end
end

#scoped(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/rails_compatable/active_record/relation.rb', line 28

def scoped(options = {})
  records = self.where(true)
  scoped_methods = %w[conditions from group having includes joins limit lock offset order readonly select]
  options.with_indifferent_access.slice(*scoped_methods).each do |method, params|
    records = records.send(method, params)
  end
  records
end

#set_table_name(name) ⇒ Object



24
25
26
# File 'lib/rails_compatable/active_record/relation.rb', line 24

def set_table_name(name)
  table_name = name
end

#take(limit = nil) ⇒ Object



8
9
10
# File 'lib/rails_compatable/active_record/relation.rb', line 8

def take(limit = nil)
  limit ? limit(limit).to_a : find_take
end