module Linker::Attributes
Constants
- USELESS_COLUMNS_REGEX
Public Instance Methods
get_main_model()
click to toggle source
# File lib/linker/forms/attributes.rb, line 10 def get_main_model @main_model ||= self.class._main_model.constantize end
prepare_attrs()
click to toggle source
# File lib/linker/forms/attributes.rb, line 14 def prepare_attrs get_main_model set_reader_for_main_model set_delegations set_fields_for_methods(map_has_many_associations) set_fields_for_methods(map_belongs_to_associations, true) set_fields_for_methods(map_has_one_associations, true) create_list_accessors_for(map_has_one_associations) create_list_accessors_for(map_has_and_belongs_to_many_associations) set_remove_accessor(map_has_many_associations) end
set_delegations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 31 def set_delegations # Delegate fields for main model filter_columns(@main_model).each do |c| delegate_attr(c, @main_model.to_s.underscore) end [map_has_one_associations, map_belongs_to_associations].each do |rgroup| rgroup.each do |c| return unless c[:columns] c[:columns].each do |cc| # ap "delegating #{cc} and #{cc}= for #{c[:name]}__#{cc}" self.class.__send__(:delegate, cc, "#{cc}=", to: c[:name].underscore.to_sym, prefix: "#{c[:name]}_") end end end end
set_reader_for_main_model()
click to toggle source
# File lib/linker/forms/attributes.rb, line 26 def set_reader_for_main_model # Create attr reader for main model self.class.__send__(:attr_reader, @main_model.to_s.underscore) end
Private Instance Methods
create_list_accessors_for(assoc_set)
click to toggle source
# File lib/linker/forms/attributes.rb, line 107 def create_list_accessors_for(assoc_set) assoc_set.each do |c| self.class.__send__(:attr_accessor, "#{c[:name]}_list") end end
delegate_attr(att, class_to)
click to toggle source
# File lib/linker/forms/attributes.rb, line 79 def delegate_attr(att, class_to) # ap "delegating #{att} and #{att}= for #{class_to.underscore.pluralize.to_sym}" self.class.__send__(:delegate, att, "#{att}=", to: class_to.underscore.to_sym) end
filter_columns(model)
click to toggle source
# File lib/linker/forms/attributes.rb, line 66 def filter_columns(model) f = model.columns.map(&:name) .delete_if { |cn| USELESS_COLUMNS_REGEX.match(cn) } # Get Paperclip attachments begin f = Paperclip::AttachmentRegistry.names_for(model).inject(f) do |t, c| t << c.to_s end rescue end f end
get_belongs_to_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 58 def get_belongs_to_associations @bt_assoc ||= @main_model.reflect_on_all_associations(:belongs_to) end
get_has_and_belongs_to_many_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 62 def get_has_and_belongs_to_many_associations @habtm_assoc ||= @main_model.reflect_on_all_associations(:has_and_belongs_to_many) end
get_has_many_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 50 def get_has_many_associations @hm_assoc ||= @main_model.reflect_on_all_associations(:has_many) end
get_has_one_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 54 def get_has_one_associations @ho_assoc ||= @main_model.reflect_on_all_associations(:has_one) end
map_associations(assoc)
click to toggle source
# File lib/linker/forms/attributes.rb, line 120 def map_associations(assoc) assoc.inject([]) do |t, c| t << { name: c.name.to_s, } unless c.options[:polymorphic] t.last[:columns] = filter_columns(c.klass) t.last[:klass] = c.klass.name end t end end
map_belongs_to_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 138 def map_belongs_to_associations # Create an array with associated classes names and attrs @mapped_bt_assoc ||= map_associations(get_belongs_to_associations) end
map_has_and_belongs_to_many_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 148 def map_has_and_belongs_to_many_associations # Create an array with associated classes names and attrs @mapped_habtm_assoc ||= map_associations(get_has_and_belongs_to_many_associations) end
map_has_many_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 133 def map_has_many_associations # Create an array with associated classes names and attrs @mapped_hm_assoc ||= map_associations(get_has_many_associations) end
map_has_one_associations()
click to toggle source
# File lib/linker/forms/attributes.rb, line 143 def map_has_one_associations # Create an array with associated classes names and attrs @mapped_ho_assoc ||= map_associations(get_has_one_associations) end
set_fields_for_methods(assoc_set, singular = false)
click to toggle source
Create required methods to use `fields_for`
# File lib/linker/forms/attributes.rb, line 85 def set_fields_for_methods(assoc_set, singular = false) assoc_set.each do |c| # ap "creating method #{c[:name]}" self.class.send(:define_method, c[:name]) do assocs = instance_variable_get("@#{get_main_model.to_s.underscore}") .send(c[:name]) neww = singular ? c[:klass].constantize.new : [c[:klass].constantize.new] * 2 if singular (assocs.present? && assocs) || neww else (assocs.map { |c| c }.present? && assocs.map { |c| c }) || neww end end # ap "creating method #{c[:name]}_attributes=" self.class.send(:define_method, "#{c[:name]}_attributes=") do |attributes| end end end
set_remove_accessor(assoc_set)
click to toggle source
# File lib/linker/forms/attributes.rb, line 113 def set_remove_accessor(assoc_set) assoc_set.each do |c| # ap "creating attr_accessor :_remove para #{c[:klass]}" c[:klass].constantize.class_eval { attr_accessor :_remove } end end