Module: Effective::Resources::Associations

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

Constant Summary collapse

INVALID_SCOPE_NAMES =
['delete_all', 'destroy_all', 'update_all', 'update_counters', 'load', 'reload', 'reset', 'to_a', 'to_sql', 'explain', 'inspect']

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributesObject



90
91
92
93
# File 'app/models/effective/resources/associations.rb', line 90

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



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

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



80
81
82
# File 'app/models/effective/resources/associations.rb', line 80

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

#active_storage(name) ⇒ Object



141
142
143
144
# File 'app/models/effective/resources/associations.rb', line 141

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



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

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



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

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



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

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



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

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

#active_storagesObject



52
53
54
55
# File 'app/models/effective/resources/associations.rb', line 52

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



95
96
97
98
# File 'app/models/effective/resources/associations.rb', line 95

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



12
13
14
15
# File 'app/models/effective/resources/associations.rb', line 12

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



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

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

#belongs_to(name) ⇒ Object



100
101
102
103
104
105
106
107
# File 'app/models/effective/resources/associations.rb', line 100

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



109
110
111
112
# File 'app/models/effective/resources/associations.rb', line 109

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



129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/effective/resources/associations.rb', line 129

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



114
115
116
117
# File 'app/models/effective/resources/associations.rb', line 114

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



47
48
49
50
# File 'app/models/effective/resources/associations.rb', line 47

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



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

def has_anys
  (has_ones + has_manys + has_and_belongs_to_manys)
end

#has_many(name) ⇒ Object



119
120
121
122
# File 'app/models/effective/resources/associations.rb', line 119

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

#has_manysObject



42
43
44
45
# File 'app/models/effective/resources/associations.rb', line 42

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



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

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



124
125
126
127
# File 'app/models/effective/resources/associations.rb', line 124

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

#has_onesObject



30
31
32
33
34
35
36
# File 'app/models/effective/resources/associations.rb', line 30

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



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

def has_ones_ids
  has_ones.map { |ass| "#{ass.name}_id".to_sym }
end

#macrosObject



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

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

#nested_resource(name) ⇒ Object



146
147
148
149
# File 'app/models/effective/resources/associations.rb', line 146

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

#nested_resourcesObject



84
85
86
87
88
# File 'app/models/effective/resources/associations.rb', line 84

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)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/effective/resources/associations.rb', line 151

def scope?(name)
  return false unless name.present?

  name = name.to_s
  return false unless klass.respond_to?(name)

  return false if INVALID_SCOPE_NAMES.include?(name)
  return false if name.include?('?') || name.include?('!') || name.include?('=')

  is_scope = false

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

    raise ActiveRecord::Rollback unless is_scope
  end

  is_scope
end