Class: ActsAsTable::ForeignKey
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActsAsTable::ForeignKey
- Defined in:
- app/models/acts_as_table/foreign_key.rb
Overview
ActsAsTable foreign key (value provider).
Constant Summary collapse
- CAPTURE_GROUP_INDEX_REGEXP =
::Regexp.new("#{Regexp.quote('\\')}(0|[1-9][0-9]*)").freeze
- POLYMORPHIC_SEPARATOR =
':'.freeze
Instance Attribute Summary collapse
-
#default_value ⇒ String?
Returns the default value for this ActsAsTable foreign key.
-
#method_name ⇒ String
Returns the method name for this ActsAsTable foreign key (a
:belongs_to
association). -
#polymorphic ⇒ Boolean
Returns 'true' if this ActsAsTable foreign key is polymorphic.
-
#primary_key ⇒ String
Returns the name of the method that returns the primary key used for the association.
Belongs to collapse
-
#column_model ⇒ ActsAsTable::ColumnModel?
Returns the ActsAsTable column model for this ActsAsTable foreign key or
nil
. -
#record_model ⇒ ActsAsTable::RecordModel
Returns the ActsAsTable record model for this ActsAsTable foreign key.
Has many collapse
-
#foreign_key_maps ⇒ ActiveRecord::Relation<ActsAsTable::ForeignKeyMap>
Returns the maps for this ActsAsTable foreign key.
-
#values ⇒ ActiveRecord::Relation<ActsAsTable::Value>
Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key.
Instance Attribute Details
#default_value ⇒ String?
Returns the default value for this ActsAsTable foreign key.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/acts_as_table/foreign_key.rb', line 20 class ForeignKey < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProvider::InstanceMethods # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.foreign_keys_table acts_as_table_value_provider # Returns the ActsAsTable column model for this ActsAsTable foreign key or `nil`. # # @return [ActsAsTable::ColumnModel, nil] belongs_to :column_model, **{ class_name: 'ActsAsTable::ColumnModel', inverse_of: :foreign_keys, required: false, } # Returns the ActsAsTable record model for this ActsAsTable foreign key. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :foreign_keys, required: true, } # Returns the maps for this ActsAsTable foreign key. has_many :foreign_key_maps, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::ForeignKeyMap', dependent: :destroy, foreign_key: 'foreign_key_id', inverse_of: :foreign_key, validate: true, } # Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key. has_many :values, **{ as: :value_provider, class_name: 'ActsAsTable::Value', dependent: :restrict_with_exception, foreign_key: 'value_provider_id', foreign_type: 'value_provider_type', inverse_of: :value_provider, } accepts_nested_attributes_for :foreign_key_maps, **{ allow_destroy: true, } validates :default_value, **{ presence: { unless: :column_model, }, } validates :method_name, **{ presence: true, } # validates :polymorphic, **{} validates :primary_key, **{ presence: true, } validate :method_name_must_be_belongs_to_association, **{ if: ::Proc.new { |foreign_key| foreign_key.method_name.present? }, } # @return [Regexp] CAPTURE_GROUP_INDEX_REGEXP = ::Regexp.new("#{Regexp.quote('\\')}(0|[1-9][0-9]*)").freeze # @return [String] POLYMORPHIC_SEPARATOR = ':'.freeze protected # @param [ActiveRecord::Base, nil] base # @return [Object, nil] def _get_value(base = nil) if base.nil? nil else base.send(:"#{self.method_name}").try { |record| record.try(:"#{self.primary_key}").try { |record_id| if self.polymorphic? record_class_name = record.class.name record_table_name = ActsAsTable.adapter.tableize_for(self, record.class.name) if record_table_name.nil? || (record_table_name.is_a?(::String) && record_table_name.include?(POLYMORPHIC_SEPARATOR)) raise "invalid record table name - #{record_table_name.inspect}" end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else record_id end } } end end # @param [Object, nil] new_value # @return [Object, nil] def _modify_set_value_before_default(new_value = nil) if new_value.nil? new_value elsif self.polymorphic? # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(record_id)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i [record_table_name, md[i]].join(POLYMORPHIC_SEPARATOR) } end end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(new_value)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i md[i] } end end new_value end end # @param [ActiveRecord::Base, nil] base # @param [Object, nil] new_value # @return [Array<Object>] def _set_value(base = nil, new_value = nil) if base.nil? [ nil, false, ] elsif self.polymorphic? # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) unless reflection.polymorphic? raise "invalid reflection (monomorphic) - #{reflection.inspect}" end # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) # @return [String] record_class_name = ActsAsTable.adapter.classify_for(self, record_table_name) # @return [ActiveRecord::Base, nil] new_target_record = record_class_name.try(:constantize).try(:"find_by_#{self.primary_key}", record_id) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ new_target_record.try { |record| [record_table_name, base.send(:"#{self.method_name}=", record).try(self.primary_key)].join(POLYMORPHIC_SEPARATOR) }, changed, ] else # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) # @return [ActiveRecord::Base, nil] new_target_record = reflection.klass.send(:"find_by_#{self.primary_key}", new_value) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ base.send(:"#{self.method_name}=", new_target_record).try(self.primary_key), changed, ] end end private # @param [Class] klass # @param [String] column_name # @return [Boolean] def _column?(klass, column_name) klass.column_names.include?(column_name.to_s) end # @return [void] def method_name_must_be_belongs_to_association self.record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |klass| # @return [ActiveRecord::Reflection::MacroReflection] reflection = klass.reflect_on_association(self.method_name) if reflection.nil? self.errors.add('method_name', :required) elsif reflection.macro == :belongs_to if self.polymorphic? unless reflection.polymorphic? self.errors.add('method_name', :invalid) end unless self.primary_key.eql?('id') self.errors.add('primary_key', :invalid) end else if reflection.polymorphic? self.errors.add('method_name', :invalid) end self.primary_key.try { |primary_key| if _column?(reflection.klass, primary_key) self.default_value.try { |default_value| unless reflection.klass.exists?({ primary_key => default_value, }) self.errors.add('default_value', :required) end } self.foreign_key_maps.each do |foreign_key_map| foreign_key_map.target_value.try { |target_value| unless reflection.klass.exists?({ primary_key => target_value, }) foreign_key_map.errors.add('target_value', :required) end } end else self.errors.add('primary_key', :required) end } end else self.errors.add('method_name', :invalid) end } return end end |
#method_name ⇒ String
Returns the method name for this ActsAsTable foreign key (a :belongs_to
association).
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/acts_as_table/foreign_key.rb', line 20 class ForeignKey < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProvider::InstanceMethods # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.foreign_keys_table acts_as_table_value_provider # Returns the ActsAsTable column model for this ActsAsTable foreign key or `nil`. # # @return [ActsAsTable::ColumnModel, nil] belongs_to :column_model, **{ class_name: 'ActsAsTable::ColumnModel', inverse_of: :foreign_keys, required: false, } # Returns the ActsAsTable record model for this ActsAsTable foreign key. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :foreign_keys, required: true, } # Returns the maps for this ActsAsTable foreign key. has_many :foreign_key_maps, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::ForeignKeyMap', dependent: :destroy, foreign_key: 'foreign_key_id', inverse_of: :foreign_key, validate: true, } # Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key. has_many :values, **{ as: :value_provider, class_name: 'ActsAsTable::Value', dependent: :restrict_with_exception, foreign_key: 'value_provider_id', foreign_type: 'value_provider_type', inverse_of: :value_provider, } accepts_nested_attributes_for :foreign_key_maps, **{ allow_destroy: true, } validates :default_value, **{ presence: { unless: :column_model, }, } validates :method_name, **{ presence: true, } # validates :polymorphic, **{} validates :primary_key, **{ presence: true, } validate :method_name_must_be_belongs_to_association, **{ if: ::Proc.new { |foreign_key| foreign_key.method_name.present? }, } # @return [Regexp] CAPTURE_GROUP_INDEX_REGEXP = ::Regexp.new("#{Regexp.quote('\\')}(0|[1-9][0-9]*)").freeze # @return [String] POLYMORPHIC_SEPARATOR = ':'.freeze protected # @param [ActiveRecord::Base, nil] base # @return [Object, nil] def _get_value(base = nil) if base.nil? nil else base.send(:"#{self.method_name}").try { |record| record.try(:"#{self.primary_key}").try { |record_id| if self.polymorphic? record_class_name = record.class.name record_table_name = ActsAsTable.adapter.tableize_for(self, record.class.name) if record_table_name.nil? || (record_table_name.is_a?(::String) && record_table_name.include?(POLYMORPHIC_SEPARATOR)) raise "invalid record table name - #{record_table_name.inspect}" end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else record_id end } } end end # @param [Object, nil] new_value # @return [Object, nil] def _modify_set_value_before_default(new_value = nil) if new_value.nil? new_value elsif self.polymorphic? # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(record_id)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i [record_table_name, md[i]].join(POLYMORPHIC_SEPARATOR) } end end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(new_value)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i md[i] } end end new_value end end # @param [ActiveRecord::Base, nil] base # @param [Object, nil] new_value # @return [Array<Object>] def _set_value(base = nil, new_value = nil) if base.nil? [ nil, false, ] elsif self.polymorphic? # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) unless reflection.polymorphic? raise "invalid reflection (monomorphic) - #{reflection.inspect}" end # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) # @return [String] record_class_name = ActsAsTable.adapter.classify_for(self, record_table_name) # @return [ActiveRecord::Base, nil] new_target_record = record_class_name.try(:constantize).try(:"find_by_#{self.primary_key}", record_id) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ new_target_record.try { |record| [record_table_name, base.send(:"#{self.method_name}=", record).try(self.primary_key)].join(POLYMORPHIC_SEPARATOR) }, changed, ] else # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) # @return [ActiveRecord::Base, nil] new_target_record = reflection.klass.send(:"find_by_#{self.primary_key}", new_value) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ base.send(:"#{self.method_name}=", new_target_record).try(self.primary_key), changed, ] end end private # @param [Class] klass # @param [String] column_name # @return [Boolean] def _column?(klass, column_name) klass.column_names.include?(column_name.to_s) end # @return [void] def method_name_must_be_belongs_to_association self.record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |klass| # @return [ActiveRecord::Reflection::MacroReflection] reflection = klass.reflect_on_association(self.method_name) if reflection.nil? self.errors.add('method_name', :required) elsif reflection.macro == :belongs_to if self.polymorphic? unless reflection.polymorphic? self.errors.add('method_name', :invalid) end unless self.primary_key.eql?('id') self.errors.add('primary_key', :invalid) end else if reflection.polymorphic? self.errors.add('method_name', :invalid) end self.primary_key.try { |primary_key| if _column?(reflection.klass, primary_key) self.default_value.try { |default_value| unless reflection.klass.exists?({ primary_key => default_value, }) self.errors.add('default_value', :required) end } self.foreign_key_maps.each do |foreign_key_map| foreign_key_map.target_value.try { |target_value| unless reflection.klass.exists?({ primary_key => target_value, }) foreign_key_map.errors.add('target_value', :required) end } end else self.errors.add('primary_key', :required) end } end else self.errors.add('method_name', :invalid) end } return end end |
#polymorphic ⇒ Boolean
Returns 'true' if this ActsAsTable foreign key is polymorphic. Otherwise, returns false
. By default, this is false
.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/acts_as_table/foreign_key.rb', line 20 class ForeignKey < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProvider::InstanceMethods # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.foreign_keys_table acts_as_table_value_provider # Returns the ActsAsTable column model for this ActsAsTable foreign key or `nil`. # # @return [ActsAsTable::ColumnModel, nil] belongs_to :column_model, **{ class_name: 'ActsAsTable::ColumnModel', inverse_of: :foreign_keys, required: false, } # Returns the ActsAsTable record model for this ActsAsTable foreign key. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :foreign_keys, required: true, } # Returns the maps for this ActsAsTable foreign key. has_many :foreign_key_maps, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::ForeignKeyMap', dependent: :destroy, foreign_key: 'foreign_key_id', inverse_of: :foreign_key, validate: true, } # Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key. has_many :values, **{ as: :value_provider, class_name: 'ActsAsTable::Value', dependent: :restrict_with_exception, foreign_key: 'value_provider_id', foreign_type: 'value_provider_type', inverse_of: :value_provider, } accepts_nested_attributes_for :foreign_key_maps, **{ allow_destroy: true, } validates :default_value, **{ presence: { unless: :column_model, }, } validates :method_name, **{ presence: true, } # validates :polymorphic, **{} validates :primary_key, **{ presence: true, } validate :method_name_must_be_belongs_to_association, **{ if: ::Proc.new { |foreign_key| foreign_key.method_name.present? }, } # @return [Regexp] CAPTURE_GROUP_INDEX_REGEXP = ::Regexp.new("#{Regexp.quote('\\')}(0|[1-9][0-9]*)").freeze # @return [String] POLYMORPHIC_SEPARATOR = ':'.freeze protected # @param [ActiveRecord::Base, nil] base # @return [Object, nil] def _get_value(base = nil) if base.nil? nil else base.send(:"#{self.method_name}").try { |record| record.try(:"#{self.primary_key}").try { |record_id| if self.polymorphic? record_class_name = record.class.name record_table_name = ActsAsTable.adapter.tableize_for(self, record.class.name) if record_table_name.nil? || (record_table_name.is_a?(::String) && record_table_name.include?(POLYMORPHIC_SEPARATOR)) raise "invalid record table name - #{record_table_name.inspect}" end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else record_id end } } end end # @param [Object, nil] new_value # @return [Object, nil] def _modify_set_value_before_default(new_value = nil) if new_value.nil? new_value elsif self.polymorphic? # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(record_id)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i [record_table_name, md[i]].join(POLYMORPHIC_SEPARATOR) } end end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(new_value)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i md[i] } end end new_value end end # @param [ActiveRecord::Base, nil] base # @param [Object, nil] new_value # @return [Array<Object>] def _set_value(base = nil, new_value = nil) if base.nil? [ nil, false, ] elsif self.polymorphic? # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) unless reflection.polymorphic? raise "invalid reflection (monomorphic) - #{reflection.inspect}" end # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) # @return [String] record_class_name = ActsAsTable.adapter.classify_for(self, record_table_name) # @return [ActiveRecord::Base, nil] new_target_record = record_class_name.try(:constantize).try(:"find_by_#{self.primary_key}", record_id) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ new_target_record.try { |record| [record_table_name, base.send(:"#{self.method_name}=", record).try(self.primary_key)].join(POLYMORPHIC_SEPARATOR) }, changed, ] else # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) # @return [ActiveRecord::Base, nil] new_target_record = reflection.klass.send(:"find_by_#{self.primary_key}", new_value) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ base.send(:"#{self.method_name}=", new_target_record).try(self.primary_key), changed, ] end end private # @param [Class] klass # @param [String] column_name # @return [Boolean] def _column?(klass, column_name) klass.column_names.include?(column_name.to_s) end # @return [void] def method_name_must_be_belongs_to_association self.record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |klass| # @return [ActiveRecord::Reflection::MacroReflection] reflection = klass.reflect_on_association(self.method_name) if reflection.nil? self.errors.add('method_name', :required) elsif reflection.macro == :belongs_to if self.polymorphic? unless reflection.polymorphic? self.errors.add('method_name', :invalid) end unless self.primary_key.eql?('id') self.errors.add('primary_key', :invalid) end else if reflection.polymorphic? self.errors.add('method_name', :invalid) end self.primary_key.try { |primary_key| if _column?(reflection.klass, primary_key) self.default_value.try { |default_value| unless reflection.klass.exists?({ primary_key => default_value, }) self.errors.add('default_value', :required) end } self.foreign_key_maps.each do |foreign_key_map| foreign_key_map.target_value.try { |target_value| unless reflection.klass.exists?({ primary_key => target_value, }) foreign_key_map.errors.add('target_value', :required) end } end else self.errors.add('primary_key', :required) end } end else self.errors.add('method_name', :invalid) end } return end end |
#primary_key ⇒ String
Returns the name of the method that returns the primary key used for the association. By default, this is id
.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'app/models/acts_as_table/foreign_key.rb', line 20 class ForeignKey < ::ActiveRecord::Base # @!parse # include ActsAsTable::ValueProvider # include ActsAsTable::ValueProvider::InstanceMethods # include ActsAsTable::ValueProviderAssociationMethods self.table_name = ActsAsTable.foreign_keys_table acts_as_table_value_provider # Returns the ActsAsTable column model for this ActsAsTable foreign key or `nil`. # # @return [ActsAsTable::ColumnModel, nil] belongs_to :column_model, **{ class_name: 'ActsAsTable::ColumnModel', inverse_of: :foreign_keys, required: false, } # Returns the ActsAsTable record model for this ActsAsTable foreign key. belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :foreign_keys, required: true, } # Returns the maps for this ActsAsTable foreign key. has_many :foreign_key_maps, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::ForeignKeyMap', dependent: :destroy, foreign_key: 'foreign_key_id', inverse_of: :foreign_key, validate: true, } # Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key. has_many :values, **{ as: :value_provider, class_name: 'ActsAsTable::Value', dependent: :restrict_with_exception, foreign_key: 'value_provider_id', foreign_type: 'value_provider_type', inverse_of: :value_provider, } accepts_nested_attributes_for :foreign_key_maps, **{ allow_destroy: true, } validates :default_value, **{ presence: { unless: :column_model, }, } validates :method_name, **{ presence: true, } # validates :polymorphic, **{} validates :primary_key, **{ presence: true, } validate :method_name_must_be_belongs_to_association, **{ if: ::Proc.new { |foreign_key| foreign_key.method_name.present? }, } # @return [Regexp] CAPTURE_GROUP_INDEX_REGEXP = ::Regexp.new("#{Regexp.quote('\\')}(0|[1-9][0-9]*)").freeze # @return [String] POLYMORPHIC_SEPARATOR = ':'.freeze protected # @param [ActiveRecord::Base, nil] base # @return [Object, nil] def _get_value(base = nil) if base.nil? nil else base.send(:"#{self.method_name}").try { |record| record.try(:"#{self.primary_key}").try { |record_id| if self.polymorphic? record_class_name = record.class.name record_table_name = ActsAsTable.adapter.tableize_for(self, record.class.name) if record_table_name.nil? || (record_table_name.is_a?(::String) && record_table_name.include?(POLYMORPHIC_SEPARATOR)) raise "invalid record table name - #{record_table_name.inspect}" end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else record_id end } } end end # @param [Object, nil] new_value # @return [Object, nil] def _modify_set_value_before_default(new_value = nil) if new_value.nil? new_value elsif self.polymorphic? # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(record_id)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i [record_table_name, md[i]].join(POLYMORPHIC_SEPARATOR) } end end [record_table_name, record_id].join(POLYMORPHIC_SEPARATOR) else self.foreign_key_maps.each do |foreign_key_map| # @return [Regexp] source_value_as_regexp = foreign_key_map.source_value_as_regexp unless (md = source_value_as_regexp.match(new_value)).nil? return foreign_key_map.target_value.gsub(CAPTURE_GROUP_INDEX_REGEXP) { |other_md| # @return [Integer] i = other_md[1].to_i md[i] } end end new_value end end # @param [ActiveRecord::Base, nil] base # @param [Object, nil] new_value # @return [Array<Object>] def _set_value(base = nil, new_value = nil) if base.nil? [ nil, false, ] elsif self.polymorphic? # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) unless reflection.polymorphic? raise "invalid reflection (monomorphic) - #{reflection.inspect}" end # @return [Array<String>] array = new_value.split(POLYMORPHIC_SEPARATOR) # @return [String] record_table_name = array[0] # @return [String] record_id = array[1..-1].join(POLYMORPHIC_SEPARATOR) # @return [String] record_class_name = ActsAsTable.adapter.classify_for(self, record_table_name) # @return [ActiveRecord::Base, nil] new_target_record = record_class_name.try(:constantize).try(:"find_by_#{self.primary_key}", record_id) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ new_target_record.try { |record| [record_table_name, base.send(:"#{self.method_name}=", record).try(self.primary_key)].join(POLYMORPHIC_SEPARATOR) }, changed, ] else # @return [Object, nil] orig_value = _get_value(base) # @return [Class] klass = self.record_model.class_name.constantize # @return [ActiveRecord::Reflection::MacroAssociation] reflection = klass.reflect_on_association(self.method_name) # @return [ActiveRecord::Base, nil] new_target_record = reflection.klass.send(:"find_by_#{self.primary_key}", new_value) # @return [Boolean] changed = !self.column_model.nil? && !(orig_value.nil? ? new_target_record.nil? : (new_target_record.nil? ? false : (orig_value == new_target_record.send(self.primary_key)))) [ base.send(:"#{self.method_name}=", new_target_record).try(self.primary_key), changed, ] end end private # @param [Class] klass # @param [String] column_name # @return [Boolean] def _column?(klass, column_name) klass.column_names.include?(column_name.to_s) end # @return [void] def method_name_must_be_belongs_to_association self.record_model.try { |record_model| record_model.class_name.constantize rescue nil }.try { |klass| # @return [ActiveRecord::Reflection::MacroReflection] reflection = klass.reflect_on_association(self.method_name) if reflection.nil? self.errors.add('method_name', :required) elsif reflection.macro == :belongs_to if self.polymorphic? unless reflection.polymorphic? self.errors.add('method_name', :invalid) end unless self.primary_key.eql?('id') self.errors.add('primary_key', :invalid) end else if reflection.polymorphic? self.errors.add('method_name', :invalid) end self.primary_key.try { |primary_key| if _column?(reflection.klass, primary_key) self.default_value.try { |default_value| unless reflection.klass.exists?({ primary_key => default_value, }) self.errors.add('default_value', :required) end } self.foreign_key_maps.each do |foreign_key_map| foreign_key_map.target_value.try { |target_value| unless reflection.klass.exists?({ primary_key => target_value, }) foreign_key_map.errors.add('target_value', :required) end } end else self.errors.add('primary_key', :required) end } end else self.errors.add('method_name', :invalid) end } return end end |
Instance Method Details
#column_model ⇒ ActsAsTable::ColumnModel?
Returns the ActsAsTable column model for this ActsAsTable foreign key or nil
.
33 34 35 36 37 |
# File 'app/models/acts_as_table/foreign_key.rb', line 33 belongs_to :column_model, **{ class_name: 'ActsAsTable::ColumnModel', inverse_of: :foreign_keys, required: false, } |
#foreign_key_maps ⇒ ActiveRecord::Relation<ActsAsTable::ForeignKeyMap>
Returns the maps for this ActsAsTable foreign key.
47 48 49 50 51 52 53 54 |
# File 'app/models/acts_as_table/foreign_key.rb', line 47 has_many :foreign_key_maps, -> { order(position: :asc) }, **{ autosave: true, class_name: 'ActsAsTable::ForeignKeyMap', dependent: :destroy, foreign_key: 'foreign_key_id', inverse_of: :foreign_key, validate: true, } |
#record_model ⇒ ActsAsTable::RecordModel
Returns the ActsAsTable record model for this ActsAsTable foreign key.
40 41 42 43 44 |
# File 'app/models/acts_as_table/foreign_key.rb', line 40 belongs_to :record_model, **{ class_name: 'ActsAsTable::RecordModel', inverse_of: :foreign_keys, required: true, } |
#values ⇒ ActiveRecord::Relation<ActsAsTable::Value>
Returns the ActsAsTable values that have been provided by this ActsAsTable foreign key.
57 58 59 60 61 62 63 64 |
# File 'app/models/acts_as_table/foreign_key.rb', line 57 has_many :values, **{ as: :value_provider, class_name: 'ActsAsTable::Value', dependent: :restrict_with_exception, foreign_key: 'value_provider_id', foreign_type: 'value_provider_type', inverse_of: :value_provider, } |