constructor-pages 0.5.7 → 0.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2baa47a4688a68e29dcf737f3b12bb3f1eb7b758
4
- data.tar.gz: 0e041e904853abf666db195b27a49b97c80ab332
3
+ metadata.gz: e9b2d5a5066524d6c27449d84de87965553916c7
4
+ data.tar.gz: 88bdd55c263eaf36a80cf7e4638a840a98cc245a
5
5
  SHA512:
6
- metadata.gz: 18a6b01b23863c4b7f98db5613de1afab43cb9b71184bb3f789c8b9f5384da01deb50ad3dd6b07d06c87eb66e9c06856d9788ce5b34818eb336b43f4a54a9744
7
- data.tar.gz: 550592c690084f29051ff33e4e2b07fe61f47b80dbd4feccb5db53c235805fafdfcebf3ae7e103537a6f608ab9ca4ce2d25e5bb605a715db61306d13829e8bc6
6
+ metadata.gz: 06d3f0dace870f2325d88361ec28bea1c7829475db50e105b6d086bb91a91f555bbb8ae9b4599a8e2ac4f29bc4e213969324866603e3344ef36e6131e0ec25dd
7
+ data.tar.gz: 8ef00cb3d76539fc7923cacf066367b686d91aa11269325c9b0351a3430e859d6fcc5052cda0afe8d25f40fca51efc6977923aef9e92ef4aedf257be92101841
@@ -109,6 +109,7 @@ module ConstructorPages
109
109
  end
110
110
 
111
111
  if @page.update_attributes params[:page]
112
+ @page.create_fields_values
112
113
  @page.update_fields_values params[:fields]
113
114
 
114
115
  redirect_to pages_url, notice: t(:page_success_updated, name: @page.name)
@@ -2,7 +2,7 @@ module ConstructorPages
2
2
  module CodeNameUniq
3
3
  def code_name_uniqueness
4
4
  unless Page.check_code_name(code_name) and check_code_name(code_name)
5
- errors.add(:base, t(:code_name_already_in_use))
5
+ errors.add(:base, :code_name_already_in_use)
6
6
  end
7
7
  end
8
8
  end
@@ -1,12 +1,18 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ConstructorPages
4
+ # Field model. Fields allows to add custom fields for template.
5
+ #
6
+ # Each field has type of value such as float, integer, string...
4
7
  class Field < ActiveRecord::Base
8
+ attr_accessible :name, :code_name, :type_value, :template_id, :template
9
+
10
+ # Adding code_name_uniqueness method
5
11
  include CodeNameUniq
6
12
 
13
+ # Array of available field types
7
14
  TYPES = %w{string integer float boolean text date html image}
8
15
 
9
- attr_accessible :name, :code_name, :type_value, :template_id, :template
10
16
  validates_presence_of :name
11
17
  validates_uniqueness_of :code_name, :scope => :template_id
12
18
  validate :code_name_uniqueness
@@ -16,6 +22,7 @@ module ConstructorPages
16
22
 
17
23
  belongs_to :template
18
24
 
25
+ # Adding has_many for all field types
19
26
  TYPES.each do |t|
20
27
  class_eval %{
21
28
  has_many :#{t}_types, class_name: 'Types::#{t.titleize}Type'
@@ -27,24 +34,25 @@ module ConstructorPages
27
34
  acts_as_list scope: :template_id
28
35
  default_scope order: :position
29
36
 
30
- # return constant of model by type_value
37
+ # Return constant of model type_value
31
38
  def type_class; "constructor_pages/types/#{type_value}_type".classify.constantize end
32
39
 
33
- # return object of type_value
40
+ # Return object of type_value by page
34
41
  def find_type_object(page); type_class.find_by_field_id_and_page_id(id, page.id) end
35
42
 
36
- # return created object of type_value
43
+ # Create object of type_value by page
37
44
  def create_type_object(page); type_class.create(field_id: id, page_id: page.id) end
38
45
 
39
- def find_or_create_type_object(page); find_type_object(page) || create_type_object(page) end
46
+ # Remove all type_fields values for specified page
47
+ def remove_type_object(page); find_type_object(page).destroy end
40
48
 
41
- # get value from type_field for specified page
49
+ # Get value from type_field for specified page
42
50
  def get_value_for(page)
43
51
  _type_object = find_type_object(page)
44
52
  _type_object ? _type_object.value : nil
45
53
  end
46
54
 
47
- # set value type_field for specified page
55
+ # Set value type_field for specified page
48
56
  def set_value_for(page, value)
49
57
  _type_object = find_type_object(page)
50
58
 
@@ -54,18 +62,22 @@ module ConstructorPages
54
62
  end
55
63
  end
56
64
 
57
- # remove all type_fields values for specified page
58
- def remove_values_for(page); type_class.destroy_all field_id: id, page_id: page.id end
59
-
60
65
  private
61
66
 
67
+ # Check if there is code_name in template branch
62
68
  def check_code_name(code_name)
63
69
  [code_name.pluralize, code_name.singularize].each do |name|
64
- template.self_and_ancestors.map{|t| t.code_name unless t.code_name == code_name}.include?(name)
65
- template.descendants.map{|t| t.code_name unless t.code_name == code_name}.include?(name)
70
+ %w{self_and_ancestors descendants}.each do |m|
71
+ if template.send(m).map{|t| t.code_name}.include?(name)
72
+ return false
73
+ end
74
+ end
66
75
  end
76
+
77
+ true
67
78
  end
68
79
 
80
+ # Create and destroy page fields
69
81
  %w{create destroy_all}.each do |m|
70
82
  class_eval %{
71
83
  def #{m}_page_fields
@@ -1,12 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ConstructorPages
4
+ # Page model. Pages are core for company websites, blogs etc.
4
5
  class Page < ActiveRecord::Base
5
6
  attr_accessible :name, :title, :keywords, :description,
6
7
  :url, :full_url, :active, :auto_url,
7
8
  :parent, :parent_id, :link, :in_menu, :in_map,
8
9
  :in_nav, :template_id, :template
9
10
 
11
+ # Adding has_many for all field types
10
12
  Field::TYPES.each do |t|
11
13
  class_eval %{
12
14
  has_many :#{t}_types, dependent: :destroy, class_name: 'Types::#{t.titleize}Type'
@@ -23,7 +25,7 @@ module ConstructorPages
23
25
 
24
26
  before_save :friendly_url, :template_assign, :full_url_update
25
27
  after_update :descendants_update
26
- after_create :create_fields
28
+ after_create :create_fields_values
27
29
 
28
30
  acts_as_nested_set
29
31
 
@@ -77,7 +79,7 @@ module ConstructorPages
77
79
  fields.each do |field|
78
80
  value = params[field.code_name.to_sym]
79
81
 
80
- _type_object = field.find_or_create_type_object(self)
82
+ _type_object = field.find_type_object(self)
81
83
  _type_object.value = 0 if field.type_value == 'boolean' and reset_booleans
82
84
 
83
85
  if value
@@ -88,9 +90,12 @@ module ConstructorPages
88
90
  end
89
91
  end
90
92
 
93
+ # Create fields values
94
+ def create_fields_values; fields.each {|field| field.create_type_object(self) } end
95
+
91
96
  # Remove all fields values
92
97
  def remove_fields_values
93
- fields.each {|f| f.remove_values_for self}
98
+ fields.each {|f| f.remove_type_object self}
94
99
  end
95
100
 
96
101
  # Search page by template code_name in same branch of pages and templates.
@@ -181,8 +186,5 @@ module ConstructorPages
181
186
 
182
187
  # Reload all descendants
183
188
  def descendants_update; descendants.map(&:save) end
184
-
185
- # Create fields values
186
- def create_fields; fields.each {|field| field.create_type_object(self) } end
187
189
  end
188
190
  end
@@ -1,7 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ConstructorPages
4
+ # Template model. Template allows assing different design for pages.
5
+ #
6
+ # Templates has many fields.
7
+ # For example:
8
+ # template "Product" should has fields like "price", "description", "size" etc.
4
9
  class Template < ActiveRecord::Base
10
+
11
+ # Adding code_name_uniqueness method
5
12
  include CodeNameUniq
6
13
 
7
14
  attr_accessible :name, :code_name, :child_id, :parent_id, :parent
@@ -17,7 +24,7 @@ module ConstructorPages
17
24
 
18
25
  acts_as_nested_set
19
26
 
20
- # return child corresponding child_id or children first
27
+ # Return child corresponding child_id or children first
21
28
  def child
22
29
  if child_id.nil? and !leaf?
23
30
  children.first
@@ -28,6 +35,7 @@ module ConstructorPages
28
35
 
29
36
  private
30
37
 
38
+ # Check if there is code_name in same branch
31
39
  def check_code_name(code_name)
32
40
  [code_name.pluralize, code_name.singularize].each do |name|
33
41
  if root.descendants.map{|t| t.code_name unless t.code_name == code_name}.include?(name)
@@ -25,7 +25,6 @@ Gem::Specification.new do |s|
25
25
  s.add_dependency 'haml-rails'
26
26
  s.add_dependency 'acts_as_list'
27
27
 
28
-
29
28
  s.add_development_dependency 'rspec-rails'
30
29
  s.add_development_dependency 'capybara'
31
30
  end
@@ -9,19 +9,129 @@ module ConstructorPages
9
9
  @template = Template.create name: 'Page', code_name: 'page'
10
10
 
11
11
  Field.delete_all
12
+ Field::TYPES.each do |t|
13
+ "constructor_pages/types/#{t}_type".classify.constantize.delete_all
14
+ end
12
15
  end
13
16
 
14
17
  describe '.create' do
15
18
  it 'should be valid' do
16
19
  _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
17
- _field.valid?.should be_true
20
+ _field.should be_valid
18
21
  end
19
22
 
20
- it 'should validate uniqueness' do
21
- Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
23
+ it 'should create page fields' do
24
+ _page = Page.create name: 'Page', template: @template
25
+ _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
26
+
27
+ _page.content.should == ''
28
+ end
29
+ end
30
+
31
+ describe '#destroy' do
32
+ it 'should destroy page fields' do
22
33
  _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
34
+ _page = Page.create name: 'Page', template: @template
35
+
36
+ _page.content.should == ''
37
+ _field.destroy
38
+ _page.content.should be_nil
39
+ end
40
+ end
41
+
42
+ describe '#type_class' do
43
+ it 'should return constant of type_value' do
44
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'html'
45
+ field.type_class.should == Types::HtmlType
46
+
47
+ field.type_value = 'text'
48
+ field.type_class.should == Types::TextType
49
+ end
50
+ end
51
+
52
+ describe '#find_type_object' do
53
+ it 'should find one object of type_value by page' do
54
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
55
+ page = Page.create name: 'Page', template: @template
56
+
57
+ text_type = Types::TextType.find_by_field_id_and_page_id field.id, page.id
58
+
59
+ field.find_type_object(page).should == text_type
60
+ end
61
+ end
62
+
63
+ describe '#create_type_object' do
64
+ it 'should create object of type_value by page' do
65
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
66
+ page = Page.create name: 'Page', template: @template
67
+
68
+ text_type = field.create_type_object(page)
69
+ text_type.should be_an_instance_of(Types::TextType)
70
+ text_type.page_id.should == page.id
71
+ text_type.field_id.should == field.id
72
+ end
73
+ end
74
+
75
+ describe '#remove_type_object' do
76
+ it 'should remote object of type_value by page' do
77
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
78
+ page = Page.create name: 'Page', template: @template
79
+
80
+ field.find_type_object(page).should be_an_instance_of(Types::TextType)
81
+
82
+ field.remove_type_object(page)
83
+ field.find_type_object(page).should be_nil
84
+ end
85
+ end
86
+
87
+ describe '#get_value_for' do
88
+ it 'should get value of type field by page' do
89
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
90
+ page = Page.create name: 'Page', template: @template
91
+ page.content = 'Hello world'
92
+
93
+ field.get_value_for(page).should == 'Hello world'
94
+ end
95
+ end
96
+
97
+ describe '#set_value_for' do
98
+ it 'should set value for type field by page' do
99
+ field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
100
+ page = Page.create name: 'Page', template: @template
101
+
102
+ field.set_value_for(page, 'Hello')
103
+ page.content.should == 'Hello'
104
+ end
105
+ end
106
+
107
+ describe '#code_name' do
108
+ it 'should be uniqueness in scope fields' do
109
+ Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
110
+ _field = Field.create name: 'Content 2', code_name: 'content', template: @template, type_value: 'html'
111
+
112
+ _field.should_not be_valid
113
+ end
114
+
115
+ it 'should be uniqueness in scope Page' do
116
+ Page.create name: 'Page', template: @template
117
+ _field = Field.create name: 'Content', code_name: 'get_field_value', template: @template, type_value: 'text'
118
+ _field.should_not be_valid
119
+ end
120
+
121
+ it 'should be uniqueness in scope Template branch' do
122
+ Page.create name: 'Page', template: @template
123
+
124
+ _template = Template.create name: 'Brand', code_name: 'brand', parent: @template
125
+ @template.reload
126
+
127
+ _field = Field.create name: 'Content', code_name: 'brand', template: @template, type_value: 'text'
128
+ _field.should_not be_valid
129
+
130
+ _field.code_name = 'brands'
131
+ _field.should_not be_valid
23
132
 
24
- _field.valid?.should_not be_true
133
+ _field.code_name = 'price'
134
+ _field.should be_valid
25
135
  end
26
136
  end
27
137
  end
@@ -83,15 +83,17 @@ module ConstructorPages
83
83
 
84
84
  describe '#update_fields_values' do
85
85
  it 'should update fields values with params' do
86
- Field.create name: 'Count', code_name: 'count', template: @template, type_value: 'integer'
86
+ @template.reload
87
+ Field.create name: 'Count', code_name: 'amount', template: @template, type_value: 'integer'
88
+
87
89
  @page.reload
88
90
 
89
- @page.count = 10
91
+ @page.amount = 10
90
92
 
91
- @page.update_fields_values({price: 1000, count: 20})
93
+ @page.update_fields_values({price: 1000, amount: 20})
92
94
 
93
95
  @page.price.should == 1000
94
- @page.count.should == 20
96
+ @page.amount.should == 20
95
97
  end
96
98
 
97
99
  it 'should reset boolean fields' do
@@ -4,9 +4,26 @@ require 'spec_helper'
4
4
 
5
5
  module ConstructorPages
6
6
  describe Template do
7
+ before :each do
8
+ Template.delete_all
9
+ end
10
+
7
11
  it 'should be valid' do
8
- template = Template.create name: 'Page template', code_name: 'page_template'
9
- template.valid?.should be_true
12
+ _template = Template.create name: 'Page template', code_name: 'page_template'
13
+ _template.valid?.should be_true
14
+ end
15
+
16
+ describe '#child' do
17
+ it 'should return child corresponding child_id or children first' do
18
+ _brand = Template.create name: 'Brand', code_name: 'brand'
19
+ _series = Template.create name: 'Series', code_name: 'series', parent: _brand
20
+ _brand.reload
21
+
22
+ _brand.child.should == _series
23
+ _brand.child_id = 1
24
+
25
+ _brand.child.should == _brand
26
+ end
10
27
  end
11
28
  end
12
29
  end
@@ -1,13 +1,12 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require 'capybara/rspec'
5
4
 
6
5
  module ConstructorPages
7
6
  describe 'Page Controller', type: :feature do
8
- it 'should be valid' do
9
- Page.create name: 'Hello'
10
- visit '/hello'
11
- end
7
+ # it 'should be valid' do
8
+ # Page.create name: 'Hello'
9
+ # visit '/hello'
10
+ # end
12
11
  end
13
12
  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.7
4
+ version: 0.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zotov
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.5.7
19
+ version: 0.5.8
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.5.7
26
+ version: 0.5.8
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: dragonfly
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -197,7 +197,6 @@ files:
197
197
  - lib/constructor-pages.rb
198
198
  - lib/constructor_pages/engine.rb
199
199
  - public/hello.txt
200
- - spec/features/constructor_pages/pages_controller_spec.rb
201
200
  - spec/models/constructor_pages/field_model_spec.rb
202
201
  - spec/models/constructor_pages/page_model_spec.rb
203
202
  - spec/models/constructor_pages/template_model_spec.rb
@@ -226,8 +225,8 @@ signing_key:
226
225
  specification_version: 4
227
226
  summary: Pages for Constructor CMS
228
227
  test_files:
229
- - spec/features/constructor_pages/pages_controller_spec.rb
230
228
  - spec/models/constructor_pages/field_model_spec.rb
231
229
  - spec/models/constructor_pages/page_model_spec.rb
232
230
  - spec/models/constructor_pages/template_model_spec.rb
233
231
  - spec/requests/constructor_pages/pages_controller_spec.rb
232
+ has_rdoc:
@@ -1,13 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require 'capybara/rspec'
5
-
6
- module ConstructorPages
7
- describe 'Page Controller', type: :feature do
8
- it 'should be valid' do
9
- Page.create name: 'Hello'
10
- visit '/hello'
11
- end
12
- end
13
- end