Module: Effective::Resources::Associations

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/associations.rb

Instance Method Summary collapse

Instance Method Details

#associated(name) ⇒ Object



34
35
36
37
# File 'app/models/effective/resources/associations.rb', line 34

def associated(name)
  name = (name.to_s.end_with?('_id') ? name.to_s[0...-3] : name).to_sym
  klass.reflect_on_all_associations.find { |ass| ass.name == name } || effective_addresses(name)
end

#belong_tosObject



9
10
11
12
# File 'app/models/effective/resources/associations.rb', line 9

def belong_tos
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  @belong_tos ||= klass.reflect_on_all_associations(:belongs_to)
end

#belongs_to(name) ⇒ Object



39
40
41
42
# File 'app/models/effective/resources/associations.rb', line 39

def belongs_to(name)
  name = (name.to_s.end_with?('_id') ? name.to_s[0...-3] : name).to_sym
  belong_tos.find { |ass| ass.name == name }
end

#belongs_to_polymorphic(name) ⇒ Object



44
45
46
47
# File 'app/models/effective/resources/associations.rb', line 44

def belongs_to_polymorphic(name)
  name = (name.to_s.end_with?('_id') ? name.to_s[0...-3] : name).to_sym
  belong_tos.find { |ass| ass.name == name && ass.options[:polymorphic] }
end

#effective_addresses(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/effective/resources/associations.rb', line 64

def effective_addresses(name)
  return unless defined?(EffectiveAddresses) && has_many(:addresses).try(:klass) == Effective::Address

  name = name.to_s.downcase
  return unless name.end_with?('_address') || name.end_with?('_addresses')

  category = name.split('_').reject { |name| name == 'address' || name == 'addresses' }.join('_')
  return unless category.present?

  Effective::Address.where(category: category).where(addressable_type: class_name)
end

#has_and_belongs_to_many(name) ⇒ Object



49
50
51
52
# File 'app/models/effective/resources/associations.rb', line 49

def has_and_belongs_to_many(name)
  name = name.to_sym
  has_and_belongs_to_manys.find { |ass| ass.name == name }
end

#has_and_belongs_to_manysObject



24
25
26
27
# File 'app/models/effective/resources/associations.rb', line 24

def has_and_belongs_to_manys
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  @has_and_belongs_to_manys ||= klass.reflect_on_all_associations(:has_and_belongs_to_many)
end

#has_many(name) ⇒ Object



54
55
56
57
# File 'app/models/effective/resources/associations.rb', line 54

def has_many(name)
  name = name.to_sym
  (has_manys + nested_resources).find { |ass| ass.name == name }
end

#has_manysObject



19
20
21
22
# File 'app/models/effective/resources/associations.rb', line 19

def has_manys
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  @has_manys ||= klass.reflect_on_all_associations(:has_many).reject { |ass| ass.options[:autosave] }
end

#has_one(name) ⇒ Object



59
60
61
62
# File 'app/models/effective/resources/associations.rb', line 59

def has_one(name)
  name = name.to_sym
  has_ones.find { |ass| ass.name == name }
end

#has_onesObject



14
15
16
17
# File 'app/models/effective/resources/associations.rb', line 14

def has_ones
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  @has_ones ||= klass.reflect_on_all_associations(:has_one)
end

#macrosObject



5
6
7
# File 'app/models/effective/resources/associations.rb', line 5

def macros
  [:belongs_to, :belongs_to_polymorphic, :has_many, :has_and_belongs_to_many, :has_one]
end

#nested_resource(name) ⇒ Object



76
77
78
79
# File 'app/models/effective/resources/associations.rb', line 76

def nested_resource(name)
  name = name.to_sym
  nested_resources.find { |ass| ass.name == name }
end

#nested_resourcesObject



29
30
31
32
# File 'app/models/effective/resources/associations.rb', line 29

def nested_resources
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  @nested_resources ||= klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] }
end