app/models/constructor_pages/field.rb in constructor-pages-0.5.5 vs app/models/constructor_pages/field.rb in constructor-pages-0.5.6

- old
+ new

@@ -14,48 +14,51 @@ belongs_to :template TYPES.each do |t| class_eval %{ - has_one :#{t}_type, class_name: 'Types::#{t.titleize}Type' + has_many :#{t}_types, class_name: 'Types::#{t.titleize}Type' } end has_many :pages, through: :template acts_as_list scope: :template_id default_scope order: :position # return constant of model by type_value - def type_model; "constructor_pages/types/#{type_value}_type".classify.constantize end + def type_class; "constructor_pages/types/#{type_value}_type".classify.constantize end - # remove all type_fields values for specified page - def remove_values_for(page); type_model.destroy_all field_id: id, page_id: page.id end + # return object of type_value + def find_type_object(page); type_class.find_by_field_id_and_page_id(id, page.id) end - def update_value(page, params) - _type_model = type_model.where(field_id: id, page_id: page.id).first_or_create + # return created object of type_value + def create_type_object(page); type_class.create(field_id: id, page_id: page.id) end - update_type_model(_type_model, type_value, params) if params + def find_or_create_type_object(page); find_type_object(page) || create_type_object(page) end + + # get value from type_field for specified page + def get_value_for(page) + _type_object = find_type_object(page) + _type_object ? _type_object.value : nil end - private + # set value type_field for specified page + def set_value_for(page, value) + _type_object = find_type_object(page) - def update_type_model(type_model, type_value, params) - type_model.value = 0 if type_value == 'boolean' - - if params[type_value] - if type_value == 'date' - value = params[type_value][id.to_s] - type_model.value = Date.new(value['date(1i)'].to_i, value['date(2i)'].to_i, value['date(3i)'].to_i).to_s - else - type_model.value = params[type_value][id.to_s] - end + if _type_object + _type_object.value = value + _type_object.save! end - - type_model.save end + # remove all type_fields values for specified page + def remove_values_for(page); type_class.destroy_all field_id: id, page_id: page.id end + + private + def method_uniqueness if Page.first.respond_to?(code_name) \ or Page.first.respond_to?(code_name.pluralize) \ or Page.first.respond_to?(code_name.singularize) \ or template.self_and_ancestors.map{|t| t.code_name unless t.code_name == code_name}.include?(code_name.pluralize) \ @@ -67,10 +70,10 @@ end %w{create destroy_all}.each do |m| class_eval %{ def #{m}_page_fields - template.pages.each {|page| type_model.#{m} page_id: page.id, field_id: id} + template.pages.each {|page| type_class.#{m} page_id: page.id, field_id: id} end } end end end \ No newline at end of file