app/models/constructor_pages/field.rb in constructor-pages-0.5.6 vs app/models/constructor_pages/field.rb in constructor-pages-0.5.7
- old
+ new
@@ -1,15 +1,17 @@
# encoding: utf-8
module ConstructorPages
class Field < ActiveRecord::Base
+ include CodeNameUniq
+
TYPES = %w{string integer float boolean text date html image}
attr_accessible :name, :code_name, :type_value, :template_id, :template
validates_presence_of :name
validates_uniqueness_of :code_name, :scope => :template_id
- validate :method_uniqueness
+ validate :code_name_uniqueness
after_create :create_page_fields
after_destroy :destroy_all_page_fields
belongs_to :template
@@ -55,18 +57,13 @@
# 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) \
- or template.self_and_ancestors.map{|t| t.code_name unless t.code_name == code_name}.include?(code_name.singularize) \
- or template.descendants.map{|t| t.code_name unless t.code_name == code_name}.include?(code_name.pluralize) \
- or template.descendants.map{|t| t.code_name unless t.code_name == code_name}.include?(code_name.singularize)
- errors.add(:base, 'Такой метод уже используется')
+ def check_code_name(code_name)
+ [code_name.pluralize, code_name.singularize].each do |name|
+ template.self_and_ancestors.map{|t| t.code_name unless t.code_name == code_name}.include?(name)
+ template.descendants.map{|t| t.code_name unless t.code_name == code_name}.include?(name)
end
end
%w{create destroy_all}.each do |m|
class_eval %{
\ No newline at end of file