Sha256: cacdf2d5edc5773d2f05bdf49923c7d972397eae22a8e8a8610795b1c5eab5d3
Contents?: true
Size: 1.22 KB
Versions: 2
Compression:
Stored size: 1.22 KB
Contents
module Coalla module Relation def multi_field(relation_name, options = {}) through_collection_name = options[:through_collection_name] || "#{self.model_name.singular}_#{relation_name}".to_sym through_class = self.reflections[through_collection_name].klass self_foreign_key = self.reflections[through_collection_name].foreign_key association_model_name = self.reflections[relation_name].source_reflection_name.to_sym association_foreign_key = self.reflections[relation_name].foreign_key tokens_attribute_name = "#{relation_name}_tokens" attr_reader tokens_attribute_name define_method "#{tokens_attribute_name}=" do |ids| new_through_collection = ids.split(',').each_with_index.map do |id, position| through_class.new(self_foreign_key => self.id, association_foreign_key => id, position: position) end self.send("#{through_collection_name}=", new_through_collection) end define_method "#{relation_name}_json" do |search_field| send(through_collection_name).map { |c| {id: c.send(association_model_name).id, name: c.send(association_model_name).send(search_field)} } end end end end ActiveRecord::Base.extend Coalla::Relation
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
coalla-cms-0.4.2.0 | lib/coalla/orm/relation.rb |
coalla-cms-0.4.4.3 | lib/coalla/orm/relation.rb |