constructor-pages 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,24 +2,25 @@
2
2
 
3
3
  module ConstructorPages
4
4
  class Field < ActiveRecord::Base
5
+ TYPES = %w{string integer float boolean text date html image}
6
+
5
7
  attr_accessible :name, :code_name, :type_value, :template_id, :template
6
8
  validates_presence_of :name
7
9
  validates_uniqueness_of :code_name, :scope => :template_id
8
10
  validate :method_uniqueness
9
11
 
10
12
  after_create :create_page_fields
11
- after_destroy :destroy_page_fields
13
+ after_destroy :destroy_all_page_fields
12
14
 
13
15
  belongs_to :template
14
16
 
15
- has_one :string_type, class_name: 'Types::StringType'
16
- has_one :integer_type, class_name: 'Types::IntegerType'
17
- has_one :float_type, class_name: 'Types::FloatType'
18
- has_one :boolean_type, class_name: 'Types::BooleanType'
19
- has_one :text_type, class_name: 'Types::TextType'
20
- has_one :date_type, class_name: 'Types::DateType'
21
- has_one :html_type, class_name: 'Types::HtmlType'
22
- has_one :image_type, class_name: 'Types::ImageType'
17
+ TYPES.each do |t|
18
+ class_eval %{
19
+ has_one :#{t}_type, class_name: 'Types::#{t.titleize}Type'
20
+ }
21
+ end
22
+
23
+ has_many :pages, through: :template
23
24
 
24
25
  acts_as_list scope: :template_id
25
26
  default_scope order: :position
@@ -63,16 +64,12 @@ module ConstructorPages
63
64
  end
64
65
  end
65
66
 
66
- def create_page_fields
67
- self.template.pages.each do |page|
68
- "constructor_pages/types/#{type_value}_type".classify.constantize.create page_id: page.id, field_id: id
69
- end
70
- end
71
-
72
- def destroy_page_fields
73
- self.template.pages.each do |page|
74
- "constructor_pages/types/#{type_value}_type".classify.constantize.destroy_all page_id: page.id, field_id: id
75
- end
67
+ %w{create destroy_all}.each do |m|
68
+ class_eval %{
69
+ def #{m}_page_fields
70
+ pages.each {|page| type_model.#{m} page_id: page.id, field_id: id}
71
+ end
72
+ }
76
73
  end
77
74
  end
78
75
  end
@@ -7,14 +7,11 @@ module ConstructorPages
7
7
  :parent, :parent_id, :link, :in_menu, :in_map,
8
8
  :in_nav, :template_id, :template
9
9
 
10
- has_many :string_types, dependent: :destroy, class_name: 'Types::StringType'
11
- has_many :float_types, dependent: :destroy, class_name: 'Types::FloatType'
12
- has_many :boolean_types, dependent: :destroy, class_name: 'Types::BooleanType'
13
- has_many :integer_types, dependent: :destroy, class_name: 'Types::IntegerType'
14
- has_many :text_types, dependent: :destroy, class_name: 'Types::TextType'
15
- has_many :date_types, dependent: :destroy, class_name: 'Types::DateType'
16
- has_many :html_types, dependent: :destroy, class_name: 'Types::HtmlType'
17
- has_many :image_types, dependent: :destroy, class_name: 'Types::ImageType'
10
+ Field::TYPES.each do |t|
11
+ class_eval %{
12
+ has_many :#{t}_types, dependent: :destroy, class_name: 'Types::#{t.titleize}Type'
13
+ }
14
+ end
18
15
 
19
16
  has_many :fields, through: :template
20
17
 
@@ -36,11 +33,11 @@ module ConstructorPages
36
33
  end
37
34
 
38
35
  def field(code_name, meth = 'value')
39
- field = Field.where(code_name: code_name, template_id: self.template_id).first
36
+ field = Field.find_by_code_name_and_template_id code_name, template_id
40
37
 
41
38
  if field
42
- f = "constructor_pages/types/#{field.type_value}_type".classify.constantize.where(field_id: field.id, page_id: self.id).first
43
- f.send(meth) if f
39
+ _field = field.type_model.find_by_field_id_and_page_id field.id, id
40
+ _field.send(meth) if _field
44
41
  end
45
42
  end
46
43
 
@@ -50,7 +47,7 @@ module ConstructorPages
50
47
  :title => self.title
51
48
  }.merge options
52
49
 
53
- self.template.fields.each do |field|
50
+ fields.each do |field|
54
51
  unless self.send(field.code_name)
55
52
  options = {field.code_name => self.send(field.code_name)}.merge options
56
53
  end
@@ -63,13 +60,12 @@ module ConstructorPages
63
60
  name = name.to_s
64
61
 
65
62
  if field(name).nil?
66
- _template = Template.find_by_code_name(name.singularize)
63
+ _template = Template.find_by_code_name name.singularize
67
64
  template_id = _template.id if _template
68
65
 
69
66
  if template_id
70
- result = []
71
- result = descendants.where(:template_id => template_id) if name == name.pluralize
72
- result = ancestors.where(:template_id => template_id).first if result.empty?
67
+ result = descendants.where(template_id: template_id) if name == name.pluralize
68
+ result = ancestors.where(template_id: template_id).first if result.empty?
73
69
  result || []
74
70
  end
75
71
  else
@@ -110,9 +106,7 @@ module ConstructorPages
110
106
  def descendants_update; descendants.map(&:save) end
111
107
 
112
108
  def create_fields
113
- template.fields.each do |field|
114
- "constructor_pages/types/#{field.type_value}_type".classify.constantize.create page_id: id, field: field
115
- end
109
+ fields.each {|field| field.type_model.create page_id: id, field_id: field.id}
116
110
  end
117
111
  end
118
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constructor-pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.5.3
21
+ version: 0.5.4
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.5.3
29
+ version: 0.5.4
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: dragonfly
32
32
  requirement: !ruby/object:Gem::Requirement