Module: Effective::Resources::Associations
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/associations.rb
Instance Method Summary collapse
- #accepts_nested_attributes ⇒ Object
- #action_texts ⇒ Object
- #action_texts_has_ones_ids ⇒ Object
- #active_storage(name) ⇒ Object
- #active_storage_has_manys ⇒ Object
- #active_storage_has_manys_ids ⇒ Object
- #active_storage_has_ones ⇒ Object
- #active_storage_has_ones_ids ⇒ Object
- #active_storages ⇒ Object
- #associated(name) ⇒ Object
- #belong_tos ⇒ Object
-
#belong_tos_ids ⇒ Object
author_id, post_id.
- #belongs_to(name) ⇒ Object
- #belongs_to_polymorphic(name) ⇒ Object
- #effective_addresses(name) ⇒ Object
- #has_and_belongs_to_many(name) ⇒ Object
- #has_and_belongs_to_manys ⇒ Object
- #has_anys ⇒ Object
- #has_many(name) ⇒ Object
- #has_manys ⇒ Object
- #has_manys_ids ⇒ Object
- #has_one(name) ⇒ Object
- #has_ones ⇒ Object
- #has_ones_ids ⇒ Object
- #macros ⇒ Object
- #nested_resource(name) ⇒ Object
- #nested_resources ⇒ Object
- #scope?(name) ⇒ Boolean
Instance Method Details
#accepts_nested_attributes ⇒ Object
88 89 90 91 |
# File 'app/models/effective/resources/associations.rb', line 88 def accepts_nested_attributes return [] unless klass.respond_to?(:reflect_on_all_associations) klass.reflect_on_all_associations.select { |ass| ass.[:autosave] } end |
#action_texts ⇒ Object
73 74 75 76 |
# File 'app/models/effective/resources/associations.rb', line 73 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_ids ⇒ Object
78 79 80 |
# File 'app/models/effective/resources/associations.rb', line 78 def action_texts_has_ones_ids action_texts.map { |ass| ass.name.to_s.gsub(/\Arich_text_/, '').to_sym } end |
#active_storage(name) ⇒ Object
139 140 141 142 |
# File 'app/models/effective/resources/associations.rb', line 139 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_manys ⇒ Object
55 56 57 58 |
# File 'app/models/effective/resources/associations.rb', line 55 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_ids ⇒ Object
60 61 62 |
# File 'app/models/effective/resources/associations.rb', line 60 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_ones ⇒ Object
64 65 66 67 |
# File 'app/models/effective/resources/associations.rb', line 64 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_ids ⇒ Object
69 70 71 |
# File 'app/models/effective/resources/associations.rb', line 69 def active_storage_has_ones_ids active_storage_has_ones.map { |ass| ass.name.to_s.gsub(/_attachment\z/, '').to_sym } end |
#active_storages ⇒ Object
50 51 52 53 |
# File 'app/models/effective/resources/associations.rb', line 50 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
93 94 95 96 |
# File 'app/models/effective/resources/associations.rb', line 93 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_tos ⇒ Object
10 11 12 13 |
# File 'app/models/effective/resources/associations.rb', line 10 def belong_tos return [] unless klass.respond_to?(:reflect_on_all_associations) klass.reflect_on_all_associations(:belongs_to) end |
#belong_tos_ids ⇒ Object
author_id, post_id
16 17 18 |
# File 'app/models/effective/resources/associations.rb', line 16 def belong_tos_ids belong_tos.map { |ass| (ass.[:foreign_key] || "#{ass.name}_id").to_sym } end |
#belongs_to(name) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'app/models/effective/resources/associations.rb', line 98 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.[:polymorphic] && ass.name == name } else belong_tos.find { |ass| !ass.[:polymorphic] && ass.klass == name.class } end end |
#belongs_to_polymorphic(name) ⇒ Object
107 108 109 110 |
# File 'app/models/effective/resources/associations.rb', line 107 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.[:polymorphic] && ass.name == name } end |
#effective_addresses(name) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/models/effective/resources/associations.rb', line 127 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
112 113 114 115 |
# File 'app/models/effective/resources/associations.rb', line 112 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_manys ⇒ Object
45 46 47 48 |
# File 'app/models/effective/resources/associations.rb', line 45 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_anys ⇒ Object
20 21 22 |
# File 'app/models/effective/resources/associations.rb', line 20 def has_anys (has_ones + has_manys + has_and_belongs_to_manys) end |
#has_many(name) ⇒ Object
117 118 119 120 |
# File 'app/models/effective/resources/associations.rb', line 117 def has_many(name) name = name.to_sym (has_manys + nested_resources).find { |ass| ass.name == name } end |
#has_manys ⇒ Object
40 41 42 43 |
# File 'app/models/effective/resources/associations.rb', line 40 def has_manys return [] unless klass.respond_to?(:reflect_on_all_associations) klass.reflect_on_all_associations(:has_many).reject { |ass| ass.[:autosave] || ass.class_name.to_s.start_with?('ActiveStorage::') } end |
#has_manys_ids ⇒ Object
24 25 26 |
# File 'app/models/effective/resources/associations.rb', line 24 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
122 123 124 125 |
# File 'app/models/effective/resources/associations.rb', line 122 def has_one(name) name = name.to_sym has_ones.find { |ass| ass.name == name } end |
#has_ones ⇒ Object
28 29 30 31 32 33 34 |
# File 'app/models/effective/resources/associations.rb', line 28 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_ids ⇒ Object
36 37 38 |
# File 'app/models/effective/resources/associations.rb', line 36 def has_ones_ids has_ones.map { |ass| "#{ass.name}_id".to_sym } end |
#macros ⇒ Object
6 7 8 |
# File 'app/models/effective/resources/associations.rb', line 6 def macros [:belongs_to, :belongs_to_polymorphic, :has_many, :has_and_belongs_to_many, :has_one] end |
#nested_resource(name) ⇒ Object
144 145 146 147 |
# File 'app/models/effective/resources/associations.rb', line 144 def nested_resource(name) name = name.to_sym nested_resources.find { |ass| ass.name == name } end |
#nested_resources ⇒ Object
82 83 84 85 86 |
# File 'app/models/effective/resources/associations.rb', line 82 def nested_resources return [] unless klass.respond_to?(:reflect_on_all_associations) klass.reflect_on_all_associations(:has_many).select { |ass| ass.[:autosave] } + klass.reflect_on_all_associations(:has_one).select { |ass| ass.[:autosave] } end |
#scope?(name) ⇒ Boolean
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/effective/resources/associations.rb', line 149 def scope?(name) return false unless klass.respond_to?(name) is_scope = false EffectiveResources.transaction(klass) do begin relation = klass.public_send(name).kind_of?(ActiveRecord::Relation) rescue => e end raise ActiveRecord::Rollback end is_scope end |