Module: Effective::Resources::Associations

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

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributesObject



87
88
89
90
# File 'app/models/effective/resources/associations.rb', line 87

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

#action_textsObject



72
73
74
75
# File 'app/models/effective/resources/associations.rb', line 72

def action_texts
  klass.reflect_on_all_associations(:has_one).select { |ass| ass.class_name == 'ActionText::RichText' } +
  klass.reflect_on_all_associations(:has_many).select { |ass| ass.class_name == 'ActionText::RichText' }
end

#action_texts_has_ones_idsObject



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

def action_texts_has_ones_ids
  action_texts.map { |ass| ass.name.to_s.gsub(/\Arich_text_/, '').to_sym }
end

#active_storage(name) ⇒ Object



138
139
140
141
# File 'app/models/effective/resources/associations.rb', line 138

def active_storage(name)
  name = name.to_sym
  active_storages.find { |ass| ass.name.to_s.gsub(/_attachment(s?)\z/, '').to_sym == name }
end

#active_storage_has_manysObject



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

def active_storage_has_manys
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  klass.reflect_on_all_associations(:has_many).select { |ass| ass.class_name == 'ActiveStorage::Attachment' }
end

#active_storage_has_manys_idsObject



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

def active_storage_has_manys_ids
  active_storage_has_manys.map { |ass| ass.name.to_s.gsub(/_attachments\z/, '').to_sym }
end

#active_storage_has_onesObject



63
64
65
66
# File 'app/models/effective/resources/associations.rb', line 63

def active_storage_has_ones
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  klass.reflect_on_all_associations(:has_one).select { |ass| ass.class_name == 'ActiveStorage::Attachment' }
end

#active_storage_has_ones_idsObject



68
69
70
# File 'app/models/effective/resources/associations.rb', line 68

def active_storage_has_ones_ids
  active_storage_has_ones.map { |ass| ass.name.to_s.gsub(/_attachment\z/, '').to_sym }
end

#active_storagesObject



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

def active_storages
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  klass.reflect_on_all_associations.select { |ass| ass.class_name == 'ActiveStorage::Attachment' }
end

#associated(name) ⇒ Object



92
93
94
95
# File 'app/models/effective/resources/associations.rb', line 92

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)
  klass.reflect_on_all_associations(:belongs_to)
end

#belong_tos_idsObject

author_id, post_id



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

def belong_tos_ids
  belong_tos.map { |ass| (ass.options[:foreign_key] || "#{ass.name}_id").to_sym }
end

#belongs_to(name) ⇒ Object



97
98
99
100
101
102
103
104
# File 'app/models/effective/resources/associations.rb', line 97

def belongs_to(name)
  if name.kind_of?(String) || name.kind_of?(Symbol)
    name = (name.to_s.end_with?('_id') ? name.to_s[0...-3] : name).to_sym
    belong_tos.find { |ass| !ass.options[:polymorphic] && ass.name == name }
  else
    belong_tos.find { |ass| !ass.options[:polymorphic] && ass.klass == name.class }
  end
end

#belongs_to_polymorphic(name) ⇒ Object



106
107
108
109
# File 'app/models/effective/resources/associations.rb', line 106

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.options[:polymorphic] && ass.name == name }
end

#effective_addresses(name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/effective/resources/associations.rb', line 126

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



111
112
113
114
# File 'app/models/effective/resources/associations.rb', line 111

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



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

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

#has_anysObject



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

def has_anys
  (has_ones + has_manys + has_and_belongs_to_manys)
end

#has_many(name) ⇒ Object



116
117
118
119
# File 'app/models/effective/resources/associations.rb', line 116

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

#has_manysObject



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

def has_manys
  return [] unless klass.respond_to?(:reflect_on_all_associations)
  klass.reflect_on_all_associations(:has_many).reject { |ass| ass.options[:autosave] || ass.class_name.to_s.start_with?('ActiveStorage::') }
end

#has_manys_idsObject



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

def has_manys_ids
  (has_manys + has_and_belongs_to_manys).map { |ass| "#{ass.plural_name.singularize}_ids".to_sym }
end

#has_one(name) ⇒ Object



121
122
123
124
# File 'app/models/effective/resources/associations.rb', line 121

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

#has_onesObject



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

def has_ones
  return [] unless klass.respond_to?(:reflect_on_all_associations)

  blacklist = ['ActiveStorage::', 'ActionText::']

  klass.reflect_on_all_associations(:has_one).reject { |ass| blacklist.any? { |val| ass.class_name.start_with?(val) } }
end

#has_ones_idsObject



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

def has_ones_ids
  has_ones.map { |ass| "#{ass.name}_id".to_sym }
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



143
144
145
146
# File 'app/models/effective/resources/associations.rb', line 143

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

#nested_resourcesObject



81
82
83
84
85
# File 'app/models/effective/resources/associations.rb', line 81

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

#scope?(name) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/effective/resources/associations.rb', line 148

def scope?(name)
  return false unless klass.respond_to?(name)

  is_scope = false

  ActiveRecord::Base.transaction do
    begin
      relation = klass.public_send(name).kind_of?(ActiveRecord::Relation)
    rescue => e
    end

    raise ActiveRecord::Rollback
  end

  is_scope
end