nocms-pages 0.1.0 → 1.0.0
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 +4 -4
- data/README.md +17 -124
- data/Rakefile +4 -1
- data/app/controllers/no_cms/pages/application_controller.rb +1 -0
- data/app/decorators/no_cms/blocks/block_decorator.rb +12 -0
- data/app/helpers/no_cms/pages/pages_helper.rb +7 -17
- data/app/models/no_cms/pages/deprecated/block.rb +45 -0
- data/app/models/no_cms/pages/page.rb +10 -5
- data/app/views/no_cms/pages/pages/show.html.erb +1 -1
- data/config/locales/en.yml +3 -0
- data/config/locales/es.yml +3 -0
- data/db/migrate/20141211224243_create_no_cms_blocks_no_cms_pages_relation.rb +11 -0
- data/db/migrate/20141211224348_destroy_no_cms_pages_blocks.rb +15 -0
- data/lib/generators/nocms/pages_generator.rb +4 -0
- data/lib/generators/nocms/templates/config/initializers/nocms/pages.rb +0 -40
- data/lib/nocms/pages/engine.rb +7 -0
- data/lib/nocms/pages/version.rb +1 -1
- data/lib/tasks/pages_tasks.rake +45 -4
- metadata +26 -9
- data/app/models/no_cms/pages/block.rb +0 -174
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb02fabad9eccd5316c27ba743e461bab475d77a
|
4
|
+
data.tar.gz: 055dd8a7dde5d2e0c937a3775e589470238f2ce2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fc360044fff30e339c8421d0b483fc24aa5e370074fd72534a431a74ceff84b27158918ca72cf80d3494eceeaa400839053eb99f2b75ee392e3d017b55904af
|
7
|
+
data.tar.gz: e909a3cfd7ec6faa68f0ec9dd92e62b9176b19b5b85427846aba08be7fc3b968ac792b717efd666965e49f63469473b5dccc128ca3318de1b5459d35f27afbe9
|
data/README.md
CHANGED
@@ -23,6 +23,7 @@ bundle install
|
|
23
23
|
And then import all the migrations:
|
24
24
|
|
25
25
|
```
|
26
|
+
rake no_cms_blocks:install:migrations
|
26
27
|
rake no_cms_pages:install:migrations
|
27
28
|
```
|
28
29
|
|
@@ -68,133 +69,23 @@ This `template` is set from the template attribute, being `show` the default val
|
|
68
69
|
|
69
70
|
### Blocks
|
70
71
|
|
71
|
-
|
72
|
+
In a previous gem version blocks were a model within the NoCms::Pages namespace, but now everything was moved to the [nocms-blocks](https://github.com/simplelogica/nocms-blocks) and in this repo there's only left a rake task for the migration.
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
In NoCMS Pages, block layouts define two main things:
|
76
|
-
|
77
|
-
1. What kind of information a block contains and other settings (i.e. cache settings).
|
78
|
-
2. How this information is displayed within the page.
|
79
|
-
|
80
|
-
Block settings are configured in the file `config/initializers/nocms/pages.rb`. Here we declare all the available layouts for a block.
|
81
|
-
|
82
|
-
The following code
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
NoCms::Pages.configure do |config|
|
86
|
-
|
87
|
-
config.block_layouts = {
|
88
|
-
'default' => {
|
89
|
-
template: 'default',
|
90
|
-
fields: {
|
91
|
-
title: :string,
|
92
|
-
body: :text
|
93
|
-
}
|
94
|
-
},
|
95
|
-
'title-3_columns' => {
|
96
|
-
template: 'title_3_columns',
|
97
|
-
fields: {
|
98
|
-
title: :string,
|
99
|
-
column_1: :text,
|
100
|
-
column_2: :text,
|
101
|
-
column_3: :text
|
102
|
-
},
|
103
|
-
},
|
104
|
-
'logo-caption' => {
|
105
|
-
template: 'logo_caption',
|
106
|
-
fields: {
|
107
|
-
caption: :string,
|
108
|
-
logo: TestImage
|
109
|
-
}
|
110
|
-
}
|
111
|
-
}
|
112
|
-
|
113
|
-
end
|
114
|
-
```
|
115
|
-
|
116
|
-
declares 3 layouts ('default', 'title-3_columns' and 'logo-caption'). Each layout has a template and some declared fields. These fields will be available in the ruby object for that block. As an example, if `@block` is an instance of the NoCms::Pages::Block model which layout attribute is set to 'default' you will be able to do `@block.title`
|
117
|
-
|
118
|
-
```ruby
|
119
|
-
block = NoCms::Pages::Block.new
|
120
|
-
block.layout = 'default'
|
121
|
-
|
122
|
-
block.title = 'a title'
|
123
|
-
block.title # => 'a title'
|
124
|
-
|
125
|
-
block.column_1 = 'a column' # => NoMethodError
|
126
|
-
block.column_1 # => NoMethodError
|
127
|
-
|
128
|
-
|
129
|
-
block.layout = 'title-3_columns'
|
130
|
-
|
131
|
-
block.title # => 'a title'
|
132
|
-
block.column_1 = 'a column'
|
133
|
-
block.column_1 # => 'a column'
|
134
|
-
block.body # => NoMethodError
|
135
|
-
|
136
|
-
block.layout = 'logo_caption'
|
137
|
-
block.title # => NoMethodError
|
138
|
-
block.logo = { name: 'testing logo' } # Currently this is the way to assign objects
|
139
|
-
block.logo.name # => 'testing logo'
|
140
|
-
block.logo.class # => TestImage
|
141
|
-
block.logo = TestImage.new name: 'testing logo' # Error! Currently assigning the object is not allowed :(
|
142
|
-
```
|
143
|
-
|
144
|
-
#### Block templates
|
145
|
-
|
146
|
-
Blocks are rendered using the `render_block` helper which controls all the logic related with renderinf a block, including fragment cache control.
|
147
|
-
|
148
|
-
In the end a partial is rendered using the block as a local variable to obtain the information. This partial must be found at `no_cms/pages/blocks` views folder and have the name configured in the `template` setting of the block. This way, rendering a 'title-3_columns' would render the partial `/no_cms/pages/blocks/title_3_columns`.
|
149
|
-
|
150
|
-
This partial is a regular Rails partial (nothing special here). AS an example, this could be the content of our `/no_cms/pages/blocks/title_3_columns.html.erb` partial:
|
151
|
-
|
152
|
-
```html
|
153
|
-
<div class='columns_block'>
|
154
|
-
<h2 class="title"><%= block.title %></h2>
|
155
|
-
<p class="column_1"><%= block.column_1 %></p>
|
156
|
-
<p class="column_2"><%= block.column_2 %></p>
|
157
|
-
<p class="column_3"><%= block.column_3 %></p>
|
158
|
-
</div>
|
159
|
-
```
|
160
|
-
|
161
|
-
As you can see, the partial has a block variable containing the block object you are rendering.
|
162
|
-
|
163
|
-
Since this is plain old rails you can do everything you can do with a partial (i.e. having a `/no_cms/pages/blocks/title_3_columns.en.html.erb` for the english version and a `/no_cms/pages/blocks/title_3_columns.es.html.erb` for the spanish one).
|
74
|
+
You can read the steps for a sucessful block migration in its own [migration documentation](doc/migration-blocks-steps.md)
|
164
75
|
|
165
76
|
### Block Cache
|
166
77
|
|
167
|
-
|
168
|
-
|
169
|
-
Cache for the blocks are configured at 3 levels:
|
78
|
+
Blocks are rendered through the `render_page_block` helper instead of the `render_block` helper from `nocms-blocks`. This helper uses the `render_block` helper but add some extra cache levels:
|
170
79
|
|
171
80
|
1. The page may have its `cache_enabled` attribute set to false. If this is the case then the cache will be disabled without any further check. This way, a page can be marked as "not cacheable" (e.g. in an admin interface) and no other setting can overwrite it.
|
172
81
|
|
173
|
-
2. The `
|
82
|
+
2. The `render_page_block` helper may be called with a `cache_enabled` option set to true or false. This option will enable/disable the cache. This allow us to render a block without using the cache (maybe on a preview action).
|
174
83
|
|
175
84
|
```ruby
|
176
85
|
render_block block, cache: false
|
177
86
|
```
|
178
87
|
|
179
|
-
3. In the
|
180
|
-
|
181
|
-
```ruby
|
182
|
-
NoCms::Pages.configure do |config|
|
183
|
-
|
184
|
-
config.block_layouts = {
|
185
|
-
'default' => {
|
186
|
-
template: 'default',
|
187
|
-
fields: {
|
188
|
-
title: :string,
|
189
|
-
body: :text
|
190
|
-
},
|
191
|
-
cache_enabled: false
|
192
|
-
}
|
193
|
-
}
|
194
|
-
end
|
195
|
-
```
|
196
|
-
|
197
|
-
4. In the blocks configuration file we can enable/disable cache for all the blocks that doesn't have a cache_enabled setting. This configuration will be stored at `NoCms::Pages.cache_enabled`
|
88
|
+
3. In the page configuration file we can enable/disable cache for all the blocks that doesn't have a cache_enabled setting. This configuration will be stored at `NoCms::Pages.cache_enabled`
|
198
89
|
|
199
90
|
```ruby
|
200
91
|
NoCms::Pages.configure do |config|
|
@@ -208,23 +99,25 @@ As a summary:
|
|
208
99
|
|
209
100
|
```ruby
|
210
101
|
|
211
|
-
b = NoCms::
|
212
|
-
|
102
|
+
b = NoCms::Blocks::Block.new layout: 'default', title: 'Foo', description: 'Bar'
|
103
|
+
page.blocks << b
|
104
|
+
page.cache_enabled # => true
|
213
105
|
NoCms::Pages.cache_enabled # => true
|
214
106
|
b.cache_enabled # => false, since the block configuration sets it to false
|
215
|
-
|
107
|
+
render_page_block b # => This won't use fragment cache since this block layout have cache disabled
|
216
108
|
|
217
|
-
b = NoCms::
|
218
|
-
|
109
|
+
b = NoCms::Blocks::Block.new layout: 'title-3_columns', title: 'Foo', description: 'Bar'
|
110
|
+
page.blocks << b
|
111
|
+
page.cache_enabled # => true
|
219
112
|
NoCms::Pages.cache_enabled # => true
|
220
113
|
b.cache_enabled # => true, since this block configuration doesn't override NoCms::Pages.cache_enabled
|
221
|
-
|
114
|
+
render_page_block b # => This will use fragment cache since, by default, it's enabled for all blocks
|
222
115
|
|
223
|
-
|
116
|
+
render_page_block b, cache_enabled: false # => This won't use fragment cache as the option in the helper overrides the block configuration
|
224
117
|
|
225
118
|
page.cache_enabled = false
|
226
|
-
|
227
|
-
|
119
|
+
render_page_block b # => This won't use fragment cache sincs it's been disabled for the page and blocks configuration has been override
|
120
|
+
render_page_block b, cache_enabled: true # => This won't use fragment cache even when saying the helper to do it. Power for the users!
|
228
121
|
|
229
122
|
```
|
230
123
|
|
data/Rakefile
CHANGED
@@ -14,8 +14,11 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
+
Bundler::GemHelper.install_tasks
|
17
18
|
|
18
|
-
|
19
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
20
|
+
load 'rails/tasks/engine.rake'
|
19
21
|
|
20
22
|
Bundler::GemHelper.install_tasks
|
21
23
|
|
24
|
+
task :default => [:"app:spec"]
|
@@ -0,0 +1,12 @@
|
|
1
|
+
NoCms::Blocks::Block.class_eval do
|
2
|
+
|
3
|
+
after_create :set_default_position
|
4
|
+
|
5
|
+
has_and_belongs_to_many :pages, class_name: 'NoCms::Pages::Page'
|
6
|
+
|
7
|
+
def set_default_position
|
8
|
+
self.update_attribute :position, (((pages.map{|p| p.blocks.pluck(:position)}).flatten.compact.max || 0) + 1) if self[:position].blank? && self.pages.exists?
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
@@ -1,23 +1,13 @@
|
|
1
1
|
module NoCms
|
2
2
|
module Pages
|
3
3
|
module PagesHelper
|
4
|
-
def
|
5
|
-
# If cache is disabled for
|
6
|
-
options
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# And now decide if we use cache or not
|
13
|
-
if options[:cache_enabled]
|
14
|
-
Rails.cache.fetch "#{block_template}/#{block.id}/#{block.updated_at.to_i}" do
|
15
|
-
render block_template, block: block
|
16
|
-
end
|
17
|
-
else
|
18
|
-
render block_template, block: block
|
19
|
-
end
|
20
|
-
|
4
|
+
def render_page_block page, block, options = {}
|
5
|
+
# If cache is disabled for all pages or is disabled for this block
|
6
|
+
# then we disable no matter what the block or the options passed have to say about it.
|
7
|
+
# This way, the user in the back has the last word about disabling cache and the NoCms::Pages engine
|
8
|
+
# control the cache over the NoCms::Block engine
|
9
|
+
options[:cache_enabled] = false if !NoCms::Pages.cache_enabled || !page.cache_enabled
|
10
|
+
render_block block, options
|
21
11
|
end
|
22
12
|
end
|
23
13
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class NoCms::Pages::Deprecated::Block < ActiveRecord::Base
|
2
|
+
|
3
|
+
self.table_name = "no_cms_pages_blocks"
|
4
|
+
|
5
|
+
|
6
|
+
belongs_to :page
|
7
|
+
belongs_to :parent, class_name: "NoCms::Pages::Deprecated::Block"
|
8
|
+
has_many :children, class_name: "NoCms::Pages::Deprecated::Block", foreign_key: 'parent_id', inverse_of: :parent, dependent: :destroy
|
9
|
+
|
10
|
+
scope :roots, ->() { where parent_id: nil }
|
11
|
+
|
12
|
+
translates :layout, :fields_info, :draft
|
13
|
+
|
14
|
+
class Translation
|
15
|
+
self.table_name = "no_cms_pages_block_translations"
|
16
|
+
serialize :fields_info, Hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def readonly?
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def position
|
24
|
+
self[:position] || 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.dump
|
28
|
+
roots.map(&:dump)
|
29
|
+
end
|
30
|
+
|
31
|
+
def dump
|
32
|
+
{
|
33
|
+
page: page,
|
34
|
+
position: position,
|
35
|
+
children: children.map(&:dump),
|
36
|
+
translations: Hash[translations.map{|t| [t.locale, {
|
37
|
+
layout: t.layout,
|
38
|
+
draft: t.draft,
|
39
|
+
fields_info: t.fields_info.to_hash
|
40
|
+
}]}]
|
41
|
+
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -12,10 +12,11 @@ module NoCms::Pages
|
|
12
12
|
|
13
13
|
acts_as_nested_set
|
14
14
|
|
15
|
-
|
15
|
+
has_and_belongs_to_many :blocks, class_name: "NoCms::Blocks::Block"
|
16
16
|
accepts_nested_attributes_for :blocks, allow_destroy: true
|
17
17
|
|
18
18
|
translates :title, :body, :slug, :path, :draft, :css_class, :css_id, :cache_enabled
|
19
|
+
accepts_nested_attributes_for :translations
|
19
20
|
|
20
21
|
validates :title, presence: true
|
21
22
|
validates :body, presence: true if NoCms::Pages.use_body?
|
@@ -28,14 +29,18 @@ module NoCms::Pages
|
|
28
29
|
|
29
30
|
def set_slug_and_path
|
30
31
|
self.slug = title.parameterize if slug.nil? && !title.nil? # If there's no slug then we create it
|
31
|
-
self.slug = title.parameterize if slug.blank? && !parent.nil? # If slug is blank and this page has a parent then we recreate it
|
32
|
-
self.slug = title.parameterize if slug.blank? && Page.home && (Page.home != self) # If slug is blank and there's already a home (and it's another page) then we recreate it
|
32
|
+
self.slug = title.parameterize if slug.blank? && !title.nil? && !parent.nil? # If slug is blank and this page has a parent then we recreate it
|
33
|
+
self.slug = title.parameterize if slug.blank? && !title.nil? && Page.home && (Page.home != self) # If slug is blank and there's already a home (and it's another page) then we recreate it
|
33
34
|
self.rebuild_path if path.nil? || attribute_changed?('slug')
|
34
35
|
end
|
35
36
|
|
36
37
|
def rebuild_path
|
37
|
-
self.
|
38
|
-
|
38
|
+
if self.persisted?
|
39
|
+
self.update_attribute :path, "#{parent.path unless parent.nil?}/#{slug}"
|
40
|
+
descendants.each(&:rebuild_path)
|
41
|
+
else
|
42
|
+
self.path = "#{parent.path unless parent.nil?}/#{slug}"
|
43
|
+
end
|
39
44
|
end
|
40
45
|
|
41
46
|
def self.templates
|
data/config/locales/en.yml
CHANGED
data/config/locales/es.yml
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
class DestroyNoCmsPagesBlocks < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
|
4
|
+
if NoCms::Blocks::Block.table_exists?
|
5
|
+
begin
|
6
|
+
Rake::Task["no_cms:pages:migrate_blocks"].invoke
|
7
|
+
rescue
|
8
|
+
Rake::Task["app:no_cms:pages:migrate_blocks"].invoke
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
drop_table :no_cms_pages_blocks
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -7,6 +7,10 @@ module NoCms
|
|
7
7
|
template "config/initializers/nocms/pages.rb", File.join(destination_root, "config", "initializers", "nocms", "pages.rb")
|
8
8
|
end
|
9
9
|
|
10
|
+
def generate_nocms_blocks_initializer
|
11
|
+
generate "nocms:blocks"
|
12
|
+
end
|
13
|
+
|
10
14
|
def self.namespace
|
11
15
|
"nocms:pages"
|
12
16
|
end
|
@@ -1,45 +1,5 @@
|
|
1
1
|
NoCms::Pages.configure do |config|
|
2
2
|
|
3
|
-
# Enable Rails fragment cache for the block templates when you call the render_block helper
|
4
|
-
# You can override this cache setting in any block configuration below or sending
|
5
|
-
# the cache option true or false when calling the block helpers
|
6
|
-
# e.g: render_block block, cache: true
|
7
|
-
# config.cache_enabled = false
|
8
|
-
|
9
|
-
# In this section we configure block layouts. It's just an array of layouts, each consisting on a hash.
|
10
|
-
# Each layout has a series of options
|
11
|
-
# E.g: config.block_layouts = {
|
12
|
-
# 'title-long_text' => {
|
13
|
-
# template: 'title-long_text', # This is the template of this block,
|
14
|
-
# # used as a partial both in the front
|
15
|
-
# # and the admin (if you use the nocms-admin-pages gem)
|
16
|
-
# fields: { # This is the list of fields a block with this layout would have
|
17
|
-
# title: :string,
|
18
|
-
# long_text: :text,
|
19
|
-
# image: Image, # You may use another ActiveRecord classes of your own
|
20
|
-
# }
|
21
|
-
# allow_nested_blocks: true, # A block with this layout may include a list of nested blocks
|
22
|
-
# # This setting is actually used by nocms-admin-pages gem to show
|
23
|
-
# # nested forms
|
24
|
-
# nest_levels: [0] # Some layout may not be nestable, or useful only in certain nesting level
|
25
|
-
# # Once again, this setting is used by nocms-admin-pages gem to hide certain
|
26
|
-
# # in nested blocks. When blank, it's assumed there's no restriction.
|
27
|
-
# cache_enabled: false # When setting cache_enabled you will be **overriding** the global cache_enabled
|
28
|
-
# # setting. If you don't set a cache setting then it will use the global cache
|
29
|
-
# # setting specified above
|
30
|
-
# },
|
31
|
-
# 'title-3_columns_text' => {
|
32
|
-
# template: 'title-3_columns_text',
|
33
|
-
# fields: {
|
34
|
-
# title: :string,
|
35
|
-
# column_1: :text,
|
36
|
-
# column_2: :text,
|
37
|
-
# column_3: :text
|
38
|
-
# }
|
39
|
-
# }
|
40
|
-
# }
|
41
|
-
# config.block_layouts = {}
|
42
|
-
|
43
3
|
# By default we use blocks to create the content of the page. If we just want a big textarea to insert the content we must set use_body to true
|
44
4
|
# config.use_body = false
|
45
5
|
|
data/lib/nocms/pages/engine.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
require 'globalize'
|
2
|
+
require 'nocms-blocks'
|
2
3
|
require 'awesome_nested_set'
|
3
4
|
|
4
5
|
module NoCms
|
5
6
|
module Pages
|
6
7
|
class Engine < ::Rails::Engine
|
7
8
|
isolate_namespace NoCms::Pages
|
9
|
+
|
10
|
+
config.to_prepare do
|
11
|
+
Dir.glob(NoCms::Pages::Engine.root + "app/decorators/**/*_decorator*.rb").each do |c|
|
12
|
+
require_dependency(c)
|
13
|
+
end
|
14
|
+
end
|
8
15
|
end
|
9
16
|
end
|
10
17
|
end
|
data/lib/nocms/pages/version.rb
CHANGED
data/lib/tasks/pages_tasks.rake
CHANGED
@@ -1,4 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
namespace :no_cms do
|
2
|
+
|
3
|
+
namespace :pages do
|
4
|
+
|
5
|
+
desc 'Task for migrating blocks from NoCms::Pages::Block model to NoCms::Blocks::Block'
|
6
|
+
task :migrate_blocks => :environment do
|
7
|
+
root_blocks = NoCms::Pages::Deprecated::Block.dump
|
8
|
+
root_blocks.each do |root_block|
|
9
|
+
import_block root_block
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def import_block block
|
14
|
+
return if block[:page].nil?
|
15
|
+
|
16
|
+
# Due to globalize we have to send the default translation via the object attributes
|
17
|
+
# instead of using the translation_attributes
|
18
|
+
default_translation = block[:translations][I18n.locale]
|
19
|
+
other_translations = block[:translations].reject{|locale, _| locale == I18n.locale }
|
20
|
+
|
21
|
+
new_block = block[:page].blocks.build position: block[:position],
|
22
|
+
translations_attributes: other_translations.map {|locale, translation|
|
23
|
+
{
|
24
|
+
locale: locale,
|
25
|
+
layout: translation[:layout],
|
26
|
+
draft: translation[:draft],
|
27
|
+
fields_info: translation[:fields_info].to_hash
|
28
|
+
}
|
29
|
+
},
|
30
|
+
layout: default_translation[:layout],
|
31
|
+
draft: default_translation[:draft],
|
32
|
+
fields_info: default_translation[:fields_info].to_hash
|
33
|
+
|
34
|
+
new_block.translations.each(&:save!)
|
35
|
+
|
36
|
+
new_block.children = block[:children].map{|b| import_block b}
|
37
|
+
new_block.save!
|
38
|
+
|
39
|
+
block[:page].blocks << new_block
|
40
|
+
new_block
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nocms-pages
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simplelogica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -34,22 +34,22 @@ dependencies:
|
|
34
34
|
name: globalize
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '4.0'
|
40
37
|
- - ">="
|
41
38
|
- !ruby/object:Gem::Version
|
42
39
|
version: 4.0.0
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '5.1'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "~>"
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '4.0'
|
50
47
|
- - ">="
|
51
48
|
- !ruby/object:Gem::Version
|
52
49
|
version: 4.0.0
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '5.1'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: awesome_nested_set
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,6 +64,20 @@ dependencies:
|
|
64
64
|
- - ">="
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: 3.0.0.rc.6
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: nocms-blocks
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.1.1
|
74
|
+
type: :runtime
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 1.1.1
|
67
81
|
- !ruby/object:Gem::Dependency
|
68
82
|
name: sqlite3
|
69
83
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,9 +106,10 @@ files:
|
|
92
106
|
- app/assets/stylesheets/pages/application.css
|
93
107
|
- app/controllers/no_cms/pages/application_controller.rb
|
94
108
|
- app/controllers/no_cms/pages/pages_controller.rb
|
109
|
+
- app/decorators/no_cms/blocks/block_decorator.rb
|
95
110
|
- app/helpers/no_cms/pages/pages_helper.rb
|
96
|
-
- app/models/no_cms/pages/block.rb
|
97
111
|
- app/models/no_cms/pages/concerns/translation_scopes.rb
|
112
|
+
- app/models/no_cms/pages/deprecated/block.rb
|
98
113
|
- app/models/no_cms/pages/page.rb
|
99
114
|
- app/views/layouts/nocms/pages/application.html.erb
|
100
115
|
- app/views/no_cms/pages/blocks/_default.html.erb
|
@@ -116,6 +131,8 @@ files:
|
|
116
131
|
- db/migrate/20140407083115_add_css_class_and_id_to_no_cms_pages_page_translations.rb
|
117
132
|
- db/migrate/20140702112813_add_cache_enabled_to_no_cms_pages_page_translations.rb
|
118
133
|
- db/migrate/20140806074811_add_layout_to_no_cms_pages_page.rb
|
134
|
+
- db/migrate/20141211224243_create_no_cms_blocks_no_cms_pages_relation.rb
|
135
|
+
- db/migrate/20141211224348_destroy_no_cms_pages_blocks.rb
|
119
136
|
- lib/generators/nocms/pages_generator.rb
|
120
137
|
- lib/generators/nocms/templates/config/initializers/nocms/pages.rb
|
121
138
|
- lib/nocms-pages.rb
|
@@ -1,174 +0,0 @@
|
|
1
|
-
module NoCms::Pages
|
2
|
-
class Block < ActiveRecord::Base
|
3
|
-
|
4
|
-
include NoCms::Pages::Concerns::TranslationScopes
|
5
|
-
|
6
|
-
scope :drafts, ->() { where_with_locale(draft: true) }
|
7
|
-
scope :no_drafts, ->() { where_with_locale(draft: false) }
|
8
|
-
scope :roots, ->() { where parent_id: nil }
|
9
|
-
belongs_to :page
|
10
|
-
|
11
|
-
belongs_to :parent, class_name: "NoCms::Pages::Block"
|
12
|
-
has_many :children, class_name: "NoCms::Pages::Block", foreign_key: 'parent_id', inverse_of: :parent, dependent: :destroy
|
13
|
-
accepts_nested_attributes_for :children, allow_destroy: true
|
14
|
-
|
15
|
-
attr_reader :cached_objects
|
16
|
-
|
17
|
-
translates :layout, :fields_info, :draft
|
18
|
-
|
19
|
-
class Translation
|
20
|
-
serialize :fields_info, Hash
|
21
|
-
end
|
22
|
-
|
23
|
-
after_initialize :set_blank_fields
|
24
|
-
after_create :set_default_position
|
25
|
-
before_save :save_related_objects
|
26
|
-
before_validation :copy_parent_page
|
27
|
-
|
28
|
-
validates :fields_info, presence: { allow_blank: true }
|
29
|
-
validates :page, :layout, presence: true
|
30
|
-
|
31
|
-
def position
|
32
|
-
self[:position] || 0
|
33
|
-
end
|
34
|
-
|
35
|
-
def layout_config
|
36
|
-
NoCms::Pages.block_layouts.stringify_keys[layout]
|
37
|
-
end
|
38
|
-
|
39
|
-
def template
|
40
|
-
layout_config[:template] if layout_config
|
41
|
-
end
|
42
|
-
|
43
|
-
def cache_enabled
|
44
|
-
return NoCms::Pages.cache_enabled unless layout_config
|
45
|
-
layout_config.has_key?(:cache_enabled) ? layout_config[:cache_enabled] : NoCms::Pages.cache_enabled
|
46
|
-
end
|
47
|
-
|
48
|
-
def has_field? field
|
49
|
-
# We have the field if...
|
50
|
-
!layout_config.nil? && # We have a layout configuration AND
|
51
|
-
(
|
52
|
-
!layout_config[:fields].symbolize_keys[field.to_sym].nil? || # We have this field OR
|
53
|
-
!layout_config[:fields].symbolize_keys[field.to_s.gsub(/\_id$/, '').to_sym].nil? # we remove the final _id and then we have the field
|
54
|
-
)
|
55
|
-
end
|
56
|
-
|
57
|
-
def field_type field
|
58
|
-
return nil unless has_field?(field)
|
59
|
-
layout_config[:fields].symbolize_keys[field.to_sym]
|
60
|
-
end
|
61
|
-
|
62
|
-
def read_field field
|
63
|
-
return nil unless has_field?(field)
|
64
|
-
|
65
|
-
value = fields_info[field.to_sym] || # first, we get the value
|
66
|
-
@cached_objects[field.to_sym] # or we get it from the cached objects
|
67
|
-
|
68
|
-
# If value is still nil, but the field exists we must get the object from the database
|
69
|
-
if value.nil?
|
70
|
-
field_type = field_type(field)
|
71
|
-
field_id = fields_info["#{field}_id".to_sym]
|
72
|
-
value = @cached_objects[field.to_sym] = field_type.find(field_id) unless field_id.nil?
|
73
|
-
end
|
74
|
-
|
75
|
-
# If value is still nil, and the field_type is an ActiveRecord class, then we
|
76
|
-
if value.nil? && field_type.is_a?(Class)
|
77
|
-
value = @cached_objects[field.to_sym] = field_type.new
|
78
|
-
end
|
79
|
-
value
|
80
|
-
end
|
81
|
-
|
82
|
-
def write_field field, value
|
83
|
-
return nil unless has_field?(field)
|
84
|
-
field_type = field_type field
|
85
|
-
# If field type is a model then we update the cached object
|
86
|
-
if field_type.is_a?(Class) && field_type < ActiveRecord::Base
|
87
|
-
# First, we initialize the object if we don't read the object (it loads it into the cached objects)
|
88
|
-
@cached_objects[field.to_sym] = field_type.new if read_field(field).nil?
|
89
|
-
# Then, assign attributes
|
90
|
-
@cached_objects[field.to_sym].assign_attributes value
|
91
|
-
else # If it's a model then a new object or update the previous one
|
92
|
-
self.fields_info = fields_info.merge field.to_sym => value # when updating through an object (i.e. the page updates through nested attributes) fields_info[field.to_sym] = value doesn't work. Kudos to Rubo for this fix
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
# In this missing method we check wether we're asking for one field
|
97
|
-
# in which case we will read or write ir
|
98
|
-
def method_missing(m, *args, &block)
|
99
|
-
# We get the name of the field stripping out the '=' for writers
|
100
|
-
field = m.to_s
|
101
|
-
write_accessor = field.ends_with? '='
|
102
|
-
field.gsub!(/\=$/, '')
|
103
|
-
|
104
|
-
# If this field actually exists, then we write it or read it.
|
105
|
-
if has_field?(field)
|
106
|
-
write_accessor ?
|
107
|
-
write_field(field, args.first) :
|
108
|
-
read_field(field.to_sym)
|
109
|
-
else
|
110
|
-
super
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
# When we are assigning attributes (this method is called in new, create...)
|
115
|
-
# we must split those fields from our current layout and those who are not
|
116
|
-
# (they must be attributes).
|
117
|
-
# Attributes are processed the usual way and fields are written later
|
118
|
-
def assign_attributes new_attributes
|
119
|
-
fields = []
|
120
|
-
|
121
|
-
set_blank_fields
|
122
|
-
|
123
|
-
# We get the layout
|
124
|
-
new_layout = new_attributes[:layout] || new_attributes['layout']
|
125
|
-
self.layout = new_layout unless new_layout.nil?
|
126
|
-
|
127
|
-
Rails.logger.info "Searching #{new_attributes.keys.inspect} fields in #{self.layout} layout"
|
128
|
-
|
129
|
-
# And now separate fields and attributes
|
130
|
-
fields = new_attributes.select{|k, _| has_field? k }.symbolize_keys
|
131
|
-
new_attributes.reject!{|k, _| has_field? k }
|
132
|
-
|
133
|
-
super(new_attributes)
|
134
|
-
|
135
|
-
Rails.logger.info "Writing #{fields.inspect} to #{self.layout} block"
|
136
|
-
|
137
|
-
fields.each do |field_name, value|
|
138
|
-
self.write_field field_name, value
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def reload
|
143
|
-
@cached_objects = {}
|
144
|
-
super
|
145
|
-
end
|
146
|
-
|
147
|
-
def copy_parent_page
|
148
|
-
self.page = parent.page unless parent.nil?
|
149
|
-
end
|
150
|
-
|
151
|
-
private
|
152
|
-
|
153
|
-
def set_blank_fields
|
154
|
-
self.fields_info ||= {}
|
155
|
-
@cached_objects ||= {}
|
156
|
-
end
|
157
|
-
|
158
|
-
def set_default_position
|
159
|
-
self.update_attribute :position, ((page.blocks.pluck(:position).compact.max || 0) + 1) if self[:position].blank?
|
160
|
-
end
|
161
|
-
|
162
|
-
def save_related_objects
|
163
|
-
# Now we save each activerecord related object
|
164
|
-
cached_objects.each do |field, object|
|
165
|
-
# Notice that we don't care if the object is actually saved
|
166
|
-
# We don't care because there may be some cases where no real information is sent to an object but something is sent (i.e. the locale in a new Globalize translation) and then the object is created empty
|
167
|
-
# When this happens if we save! the object an error is thrown and we can't leave the object blank
|
168
|
-
if object.is_a?(ActiveRecord::Base) && object.save
|
169
|
-
fields_info["#{field}_id".to_sym] = object.id
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|