diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..32784b3 --- /dev/null +++ b/Appraisals @@ -0,0 +1,11 @@ +appraise "rails-4-0" do + gem "rails", "4.0.13" +end + +appraise "rails-4-1" do + gem "rails", "4.1.13" +end + +appraise "rails-4-2" do + gem "rails", "4.2.3" +end diff --git a/Gemfile b/Gemfile index fbefdb3..a83fd7f 100644 --- a/Gemfile +++ b/Gemfile @@ -23,4 +23,5 @@ group :test do gem 'factory_girl' gem 'capybara' gem 'database_cleaner' + gem 'appraisal' end diff --git a/Gemfile.lock b/Gemfile.lock index 074bad3..68d6ba5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,10 @@ PATH remote: . specs: - nocms-pages (0.0.1) + nocms-pages (1.0.0) awesome_nested_set (>= 3.0.0.rc.6) - globalize (~> 4.0, >= 4.0.0) + globalize (>= 4.0.0, < 5.1) + nocms-blocks (~> 1.1.1) rails (~> 4.0, >= 4.0.3) GEM @@ -26,15 +27,19 @@ GEM activerecord-deprecated_finders (~> 1.0.2) activesupport (= 4.0.3) arel (~> 4.0.0) - activerecord-deprecated_finders (1.0.3) + activerecord-deprecated_finders (1.0.4) activesupport (4.0.3) i18n (~> 0.6, >= 0.6.4) minitest (~> 4.2) multi_json (~> 1.3) thread_safe (~> 0.1) tzinfo (~> 0.3.37) + appraisal (2.1.0) + bundler + rake + thor (>= 0.14.0) arel (4.0.2) - awesome_nested_set (3.0.0) + awesome_nested_set (3.0.2) activerecord (>= 4.0.0, < 5) builder (3.1.4) capybara (2.2.1) @@ -55,7 +60,7 @@ GEM activesupport (>= 3.0.0) faker (1.2.0) i18n (~> 0.5) - globalize (4.0.0) + globalize (4.0.3) activemodel (>= 4.0.0, < 5) activerecord (>= 4.0.0, < 5) hike (1.2.3) @@ -68,9 +73,13 @@ GEM mini_portile (0.5.2) minitest (4.7.5) multi_json (1.10.1) + nocms-blocks (1.1.1) + awesome_nested_set (~> 3.0.0) + globalize (>= 4.0.0, < 5.1) + rails (~> 4.0, <= 4.2.3) nokogiri (1.6.1) mini_portile (~> 0.5.0) - polyglot (0.3.4) + polyglot (0.3.5) rack (1.5.2) rack-test (0.6.2) rack (>= 1.0) @@ -108,7 +117,7 @@ GEM rspec-mocks (= 3.0.0.beta2) rspec-support (= 3.0.0.beta2) rspec-support (3.0.0.beta2) - sprockets (2.11.0) + sprockets (2.12.4) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) @@ -132,6 +141,7 @@ PLATFORMS ruby DEPENDENCIES + appraisal capybara carrierwave database_cleaner diff --git a/README.md b/README.md index 9ab2e4d..bf747ea 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ bundle install And then import all the migrations: ``` +rake no_cms_blocks:install:migrations rake no_cms_pages:install:migrations ``` @@ -68,133 +69,23 @@ This `template` is set from the template attribute, being `show` the default val ### Blocks -Blocks are the unit of contents the pages are made of. They are thought to be independent and customizable modules that can be created, edited or removed on their own, without dependency of any other module. +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. -#### Block layouts - -In NoCMS Pages, block layouts define two main things: - -1. What kind of information a block contains and other settings (i.e. cache settings). -2. How this information is displayed within the page. - -Block settings are configured in the file `config/initializers/nocms/pages.rb`. Here we declare all the available layouts for a block. - -The following code - -```ruby -NoCms::Pages.configure do |config| - - config.block_layouts = { - 'default' => { - template: 'default', - fields: { - title: :string, - body: :text - } - }, - 'title-3_columns' => { - template: 'title_3_columns', - fields: { - title: :string, - column_1: :text, - column_2: :text, - column_3: :text - }, - }, - 'logo-caption' => { - template: 'logo_caption', - fields: { - caption: :string, - logo: TestImage - } - } - } - -end -``` - -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` - -```ruby -block = NoCms::Pages::Block.new -block.layout = 'default' - -block.title = 'a title' -block.title # => 'a title' - -block.column_1 = 'a column' # => NoMethodError -block.column_1 # => NoMethodError - - -block.layout = 'title-3_columns' - -block.title # => 'a title' -block.column_1 = 'a column' -block.column_1 # => 'a column' -block.body # => NoMethodError - -block.layout = 'logo_caption' -block.title # => NoMethodError -block.logo = { name: 'testing logo' } # Currently this is the way to assign objects -block.logo.name # => 'testing logo' -block.logo.class # => TestImage -block.logo = TestImage.new name: 'testing logo' # Error! Currently assigning the object is not allowed :( -``` - -#### Block templates - -Blocks are rendered using the `render_block` helper which controls all the logic related with renderinf a block, including fragment cache control. - -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`. - -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: - -```html -
-

<%= block.title %>

-

<%= block.column_1 %>

-

<%= block.column_2 %>

-

<%= block.column_3 %>

-
-``` - -As you can see, the partial has a block variable containing the block object you are rendering. - -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). +You can read the steps for a sucessful block migration in its own [migration documentation](doc/migration-blocks-steps.md) ### Block Cache -Since blocks are independent units of content within a page, the standard Rails fragment cache seemed to fit well with them. That's why the `render_block` helper decides wether Rails cache should be used for rendering an individual block. - -Cache for the blocks are configured at 3 levels: +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: 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. -2. The `render_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). +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). ```ruby render_block block, cache: false ``` -3. In the blocks configuration we can enable/disable the cache for all the blocks of a kind. We just have to add the cache_enabled setting. - - ```ruby - NoCms::Pages.configure do |config| - - config.block_layouts = { - 'default' => { - template: 'default', - fields: { - title: :string, - body: :text - }, - cache_enabled: false - } - } - end - ``` - -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` +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` ```ruby NoCms::Pages.configure do |config| @@ -208,23 +99,25 @@ As a summary: ```ruby - b = NoCms::Pages::Block.new layout: 'default', title: 'Foo', description: 'Bar', page: page - b.page.cache_enabled # => true + b = NoCms::Blocks::Block.new layout: 'default', title: 'Foo', description: 'Bar' + page.blocks << b + page.cache_enabled # => true NoCms::Pages.cache_enabled # => true b.cache_enabled # => false, since the block configuration sets it to false - render_block b # => This won't use fragment cache since this block layout have cache disabled + render_page_block b # => This won't use fragment cache since this block layout have cache disabled - b = NoCms::Pages::Block.new layout: 'title-3_columns', title: 'Foo', description: 'Bar', page: page - b.page.cache_enabled # => true + b = NoCms::Blocks::Block.new layout: 'title-3_columns', title: 'Foo', description: 'Bar' + page.blocks << b + page.cache_enabled # => true NoCms::Pages.cache_enabled # => true b.cache_enabled # => true, since this block configuration doesn't override NoCms::Pages.cache_enabled - render_block b # => This will use fragment cache since, by default, it's enabled for all blocks + render_page_block b # => This will use fragment cache since, by default, it's enabled for all blocks - render_block b, cache_enabled: false # => This won't use fragment cache as the option in the helper overrides the block configuration + render_page_block b, cache_enabled: false # => This won't use fragment cache as the option in the helper overrides the block configuration page.cache_enabled = false - render_block b # => This won't use fragment cache sincs it's been disabled for the page and blocks configuration has been override - render_block b, cache_enabled: true # => This won't use fragment cache even when saying the helper to do it. Power for the users! + render_page_block b # => This won't use fragment cache sincs it's been disabled for the page and blocks configuration has been override + 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! ``` diff --git a/Rakefile b/Rakefile index 84becba..33383f8 100644 --- a/Rakefile +++ b/Rakefile @@ -14,8 +14,11 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end +Bundler::GemHelper.install_tasks - +APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__) +load 'rails/tasks/engine.rake' Bundler::GemHelper.install_tasks +task :default => [:"app:spec"] diff --git a/app/controllers/no_cms/pages/application_controller.rb b/app/controllers/no_cms/pages/application_controller.rb index 8e33d9b..febffb5 100644 --- a/app/controllers/no_cms/pages/application_controller.rb +++ b/app/controllers/no_cms/pages/application_controller.rb @@ -1,6 +1,7 @@ module NoCms module Pages class ApplicationController < ::ApplicationController + helper NoCms::Blocks::BlocksHelper end end end diff --git a/app/decorators/no_cms/blocks/block_decorator.rb b/app/decorators/no_cms/blocks/block_decorator.rb new file mode 100644 index 0000000..1bd342c --- /dev/null +++ b/app/decorators/no_cms/blocks/block_decorator.rb @@ -0,0 +1,12 @@ +NoCms::Blocks::Block.class_eval do + + after_create :set_default_position + + has_and_belongs_to_many :pages, class_name: 'NoCms::Pages::Page' + + def set_default_position + self.update_attribute :position, (((pages.map{|p| p.blocks.pluck(:position)}).flatten.compact.max || 0) + 1) if self[:position].blank? && self.pages.exists? + end + +end + diff --git a/app/helpers/no_cms/pages/pages_helper.rb b/app/helpers/no_cms/pages/pages_helper.rb index 2d6de18..7fefa3e 100644 --- a/app/helpers/no_cms/pages/pages_helper.rb +++ b/app/helpers/no_cms/pages/pages_helper.rb @@ -1,23 +1,13 @@ module NoCms module Pages module PagesHelper - def render_block block, options = {} - # If cache is disabled for this block then we disable no matter what the block or the options passed have to say about it. This way, the user in the back has the last word about disabling cache - options[:cache_enabled] = false unless block.page.cache_enabled - # If we don't have any option about cache enabled then we ask the block - options[:cache_enabled] = block.cache_enabled unless options.has_key? :cache_enabled - - block_template = "no_cms/pages/blocks/#{block.template}" - - # And now decide if we use cache or not - if options[:cache_enabled] - Rails.cache.fetch "#{block_template}/#{block.id}/#{block.updated_at.to_i}" do - render block_template, block: block - end - else - render block_template, block: block - end - + def render_page_block page, block, options = {} + # If cache is disabled for all pages or is disabled for this block + # then we disable no matter what the block or the options passed have to say about it. + # This way, the user in the back has the last word about disabling cache and the NoCms::Pages engine + # control the cache over the NoCms::Block engine + options[:cache_enabled] = false if !NoCms::Pages.cache_enabled || !page.cache_enabled + render_block block, options end end end diff --git a/app/models/no_cms/pages/block.rb b/app/models/no_cms/pages/block.rb deleted file mode 100644 index 7e83209..0000000 --- a/app/models/no_cms/pages/block.rb +++ /dev/null @@ -1,174 +0,0 @@ -module NoCms::Pages - class Block < ActiveRecord::Base - - include NoCms::Pages::Concerns::TranslationScopes - - scope :drafts, ->() { where_with_locale(draft: true) } - scope :no_drafts, ->() { where_with_locale(draft: false) } - scope :roots, ->() { where parent_id: nil } - belongs_to :page - - belongs_to :parent, class_name: "NoCms::Pages::Block" - has_many :children, class_name: "NoCms::Pages::Block", foreign_key: 'parent_id', inverse_of: :parent, dependent: :destroy - accepts_nested_attributes_for :children, allow_destroy: true - - attr_reader :cached_objects - - translates :layout, :fields_info, :draft - - class Translation - serialize :fields_info, Hash - end - - after_initialize :set_blank_fields - after_create :set_default_position - before_save :save_related_objects - before_validation :copy_parent_page - - validates :fields_info, presence: { allow_blank: true } - validates :page, :layout, presence: true - - def position - self[:position] || 0 - end - - def layout_config - NoCms::Pages.block_layouts.stringify_keys[layout] - end - - def template - layout_config[:template] if layout_config - end - - def cache_enabled - return NoCms::Pages.cache_enabled unless layout_config - layout_config.has_key?(:cache_enabled) ? layout_config[:cache_enabled] : NoCms::Pages.cache_enabled - end - - def has_field? field - # We have the field if... - !layout_config.nil? && # We have a layout configuration AND - ( - !layout_config[:fields].symbolize_keys[field.to_sym].nil? || # We have this field OR - !layout_config[:fields].symbolize_keys[field.to_s.gsub(/\_id$/, '').to_sym].nil? # we remove the final _id and then we have the field - ) - end - - def field_type field - return nil unless has_field?(field) - layout_config[:fields].symbolize_keys[field.to_sym] - end - - def read_field field - return nil unless has_field?(field) - - value = fields_info[field.to_sym] || # first, we get the value - @cached_objects[field.to_sym] # or we get it from the cached objects - - # If value is still nil, but the field exists we must get the object from the database - if value.nil? - field_type = field_type(field) - field_id = fields_info["#{field}_id".to_sym] - value = @cached_objects[field.to_sym] = field_type.find(field_id) unless field_id.nil? - end - - # If value is still nil, and the field_type is an ActiveRecord class, then we - if value.nil? && field_type.is_a?(Class) - value = @cached_objects[field.to_sym] = field_type.new - end - value - end - - def write_field field, value - return nil unless has_field?(field) - field_type = field_type field - # If field type is a model then we update the cached object - if field_type.is_a?(Class) && field_type < ActiveRecord::Base - # First, we initialize the object if we don't read the object (it loads it into the cached objects) - @cached_objects[field.to_sym] = field_type.new if read_field(field).nil? - # Then, assign attributes - @cached_objects[field.to_sym].assign_attributes value - else # If it's a model then a new object or update the previous one - 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 - end - end - - # In this missing method we check wether we're asking for one field - # in which case we will read or write ir - def method_missing(m, *args, &block) - # We get the name of the field stripping out the '=' for writers - field = m.to_s - write_accessor = field.ends_with? '=' - field.gsub!(/\=$/, '') - - # If this field actually exists, then we write it or read it. - if has_field?(field) - write_accessor ? - write_field(field, args.first) : - read_field(field.to_sym) - else - super - end - end - - # When we are assigning attributes (this method is called in new, create...) - # we must split those fields from our current layout and those who are not - # (they must be attributes). - # Attributes are processed the usual way and fields are written later - def assign_attributes new_attributes - fields = [] - - set_blank_fields - - # We get the layout - new_layout = new_attributes[:layout] || new_attributes['layout'] - self.layout = new_layout unless new_layout.nil? - - Rails.logger.info "Searching #{new_attributes.keys.inspect} fields in #{self.layout} layout" - - # And now separate fields and attributes - fields = new_attributes.select{|k, _| has_field? k }.symbolize_keys - new_attributes.reject!{|k, _| has_field? k } - - super(new_attributes) - - Rails.logger.info "Writing #{fields.inspect} to #{self.layout} block" - - fields.each do |field_name, value| - self.write_field field_name, value - end - end - - def reload - @cached_objects = {} - super - end - - def copy_parent_page - self.page = parent.page unless parent.nil? - end - - private - - def set_blank_fields - self.fields_info ||= {} - @cached_objects ||= {} - end - - def set_default_position - self.update_attribute :position, ((page.blocks.pluck(:position).compact.max || 0) + 1) if self[:position].blank? - end - - def save_related_objects - # Now we save each activerecord related object - cached_objects.each do |field, object| - # Notice that we don't care if the object is actually saved - # 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 - # When this happens if we save! the object an error is thrown and we can't leave the object blank - if object.is_a?(ActiveRecord::Base) && object.save - fields_info["#{field}_id".to_sym] = object.id - end - end - end - end -end diff --git a/app/models/no_cms/pages/deprecated/block.rb b/app/models/no_cms/pages/deprecated/block.rb new file mode 100644 index 0000000..fee9d0c --- /dev/null +++ b/app/models/no_cms/pages/deprecated/block.rb @@ -0,0 +1,45 @@ +class NoCms::Pages::Deprecated::Block < ActiveRecord::Base + + self.table_name = "no_cms_pages_blocks" + + + belongs_to :page + belongs_to :parent, class_name: "NoCms::Pages::Deprecated::Block" + has_many :children, class_name: "NoCms::Pages::Deprecated::Block", foreign_key: 'parent_id', inverse_of: :parent, dependent: :destroy + + scope :roots, ->() { where parent_id: nil } + + translates :layout, :fields_info, :draft + + class Translation + self.table_name = "no_cms_pages_block_translations" + serialize :fields_info, Hash + end + + def readonly? + true + end + + def position + self[:position] || 0 + end + + def self.dump + roots.map(&:dump) + end + + def dump + { + page: page, + position: position, + children: children.map(&:dump), + translations: Hash[translations.map{|t| [t.locale, { + layout: t.layout, + draft: t.draft, + fields_info: t.fields_info.to_hash + }]}] + + } + end + +end diff --git a/app/models/no_cms/pages/page.rb b/app/models/no_cms/pages/page.rb index 779c56a..afa1243 100644 --- a/app/models/no_cms/pages/page.rb +++ b/app/models/no_cms/pages/page.rb @@ -12,10 +12,11 @@ def self.home acts_as_nested_set - has_many :blocks, inverse_of: :page, class_name: 'NoCms::Pages::Block' + has_and_belongs_to_many :blocks, class_name: "NoCms::Blocks::Block" accepts_nested_attributes_for :blocks, allow_destroy: true translates :title, :body, :slug, :path, :draft, :css_class, :css_id, :cache_enabled + accepts_nested_attributes_for :translations validates :title, presence: true validates :body, presence: true if NoCms::Pages.use_body? @@ -28,14 +29,18 @@ def self.home def set_slug_and_path self.slug = title.parameterize if slug.nil? && !title.nil? # If there's no slug then we create it - self.slug = title.parameterize if slug.blank? && !parent.nil? # If slug is blank and this page has a parent then we recreate it - 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 + 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 + 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 self.rebuild_path if path.nil? || attribute_changed?('slug') end def rebuild_path - self.update_attribute :path, "#{parent.path unless parent.nil?}/#{slug}" - descendants.each(&:rebuild_path) + if self.persisted? + self.update_attribute :path, "#{parent.path unless parent.nil?}/#{slug}" + descendants.each(&:rebuild_path) + else + self.path = "#{parent.path unless parent.nil?}/#{slug}" + end end def self.templates diff --git a/app/views/no_cms/pages/pages/show.html.erb b/app/views/no_cms/pages/pages/show.html.erb index 221721f..dbedf06 100644 --- a/app/views/no_cms/pages/pages/show.html.erb +++ b/app/views/no_cms/pages/pages/show.html.erb @@ -2,5 +2,5 @@

<%= @page.body %>

<% @blocks.sort_by(&:position).each do |block| %> - <%= render_block block %> + <%= render_page_block @page, block %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 74db9f9..48f5c37 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -11,6 +11,9 @@ en: css_class: CSS classes css_id: CSS id layout: Layout + parent: Parent + template: Template + draft: Draft no_cms/pages/block: layout: Layout title: Title diff --git a/config/locales/es.yml b/config/locales/es.yml index 126009f..98cbb36 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -11,6 +11,9 @@ es: css_class: Clases CSS css_id: Id CSS layout: Layout + parent: Padre + template: Plantilla + draft: Borrador no_cms/pages/block: layout: Layout title: Título diff --git a/db/migrate/20141211224243_create_no_cms_blocks_no_cms_pages_relation.rb b/db/migrate/20141211224243_create_no_cms_blocks_no_cms_pages_relation.rb new file mode 100644 index 0000000..a2e6936 --- /dev/null +++ b/db/migrate/20141211224243_create_no_cms_blocks_no_cms_pages_relation.rb @@ -0,0 +1,11 @@ +class CreateNoCmsBlocksNoCmsPagesRelation < ActiveRecord::Migration + def change + create_table :no_cms_blocks_blocks_pages_pages, id: false do |t| + + t.belongs_to :page + t.belongs_to :block + + t.timestamps + end + end +end diff --git a/db/migrate/20141211224348_destroy_no_cms_pages_blocks.rb b/db/migrate/20141211224348_destroy_no_cms_pages_blocks.rb new file mode 100644 index 0000000..d16a3a1 --- /dev/null +++ b/db/migrate/20141211224348_destroy_no_cms_pages_blocks.rb @@ -0,0 +1,15 @@ +class DestroyNoCmsPagesBlocks < ActiveRecord::Migration + def change + + if NoCms::Blocks::Block.table_exists? + begin + Rake::Task["no_cms:pages:migrate_blocks"].invoke + rescue + Rake::Task["app:no_cms:pages:migrate_blocks"].invoke + end + end + + drop_table :no_cms_pages_blocks + + end +end diff --git a/doc/migration-blocks-steps.md b/doc/migration-blocks-steps.md new file mode 100644 index 0000000..902b94d --- /dev/null +++ b/doc/migration-blocks-steps.md @@ -0,0 +1,11 @@ +These are the steps for the migration from NoCms::Pages::Block model to NoCms::Blocks::Block one + +1. *Change render_block for render_page_block*: This way, the helper we will be able to use the configuration about cache inside the own page. + +2. *Change all render that points to the no_cms/pages folder for the usual render_page_block*: If you have any `render partial: '...'` you should chante to the `render_page_block` helper, so you can benefit from the cache settings. + +3. *Change block templates from no_cms/pages folder to no_cms/blocks*: `render_block` and `render_page_blocks` will search the templates inside the no_cms/blocks views folder. + +4. If you want you can comment the `drop_table :no_cms_pages_blocks` inside the destroy_no_cms_pages_blocks migration. This way you won't delete that table and will always beable to recover the information. + +5. *Run the migrations*: the destroy_no_cms_pages_blocks migration will copy your information from the old blocks table to the new one and no information will be lost! diff --git a/gemfiles/rails_4_0.gemfile b/gemfiles/rails_4_0.gemfile new file mode 100644 index 0000000..9a244ec --- /dev/null +++ b/gemfiles/rails_4_0.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", "4.0.13" + +group :development, :test do + gem "faker" + gem "carrierwave" +end + +group :test do + gem "rspec-rails", "~> 3.0.0.beta" + gem "factory_girl" + gem "capybara" + gem "database_cleaner" + gem "appraisal" +end + +gemspec :path => "../" diff --git a/gemfiles/rails_4_0.gemfile.lock b/gemfiles/rails_4_0.gemfile.lock new file mode 100644 index 0000000..c443ea0 --- /dev/null +++ b/gemfiles/rails_4_0.gemfile.lock @@ -0,0 +1,139 @@ +PATH + remote: ../ + specs: + nocms-pages (1.0.0) + awesome_nested_set (>= 3.0.0.rc.6) + globalize (>= 4.0.0, < 5.1) + nocms-blocks (~> 1.1.1) + rails (~> 4.0, >= 4.0.3) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.0.13) + actionpack (= 4.0.13) + mail (~> 2.5, >= 2.5.4) + actionpack (4.0.13) + activesupport (= 4.0.13) + builder (~> 3.1.0) + erubis (~> 2.7.0) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + activemodel (4.0.13) + activesupport (= 4.0.13) + builder (~> 3.1.0) + activerecord (4.0.13) + activemodel (= 4.0.13) + activerecord-deprecated_finders (~> 1.0.2) + activesupport (= 4.0.13) + arel (~> 4.0.0) + activerecord-deprecated_finders (1.0.4) + activesupport (4.0.13) + i18n (~> 0.6, >= 0.6.9) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + appraisal (2.1.0) + bundler + rake + thor (>= 0.14.0) + arel (4.0.2) + awesome_nested_set (3.0.2) + activerecord (>= 4.0.0, < 5) + builder (3.1.4) + capybara (2.5.0) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) + database_cleaner (1.5.0) + diff-lcs (1.2.5) + erubis (2.7.0) + factory_girl (4.5.0) + activesupport (>= 3.0.0) + faker (1.5.0) + i18n (~> 0.5) + globalize (4.0.3) + activemodel (>= 4.0.0, < 5) + activerecord (>= 4.0.0, < 5) + i18n (0.7.0) + json (1.8.3) + mail (2.6.3) + mime-types (>= 1.16, < 3) + mime-types (2.6.1) + mini_portile (0.6.2) + minitest (4.7.5) + multi_json (1.11.2) + nocms-blocks (1.1.1) + awesome_nested_set (~> 3.0.0) + globalize (>= 4.0.0, < 5.1) + rails (~> 4.0, <= 4.2.3) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) + rack (1.5.5) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.0.13) + actionmailer (= 4.0.13) + actionpack (= 4.0.13) + activerecord (= 4.0.13) + activesupport (= 4.0.13) + bundler (>= 1.3.0, < 2.0) + railties (= 4.0.13) + sprockets-rails (~> 2.0) + railties (4.0.13) + actionpack (= 4.0.13) + activesupport (= 4.0.13) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.4.2) + rspec-core (3.0.4) + rspec-support (~> 3.0.0) + rspec-expectations (3.0.4) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.0.0) + rspec-mocks (3.0.4) + rspec-support (~> 3.0.0) + rspec-rails (3.0.2) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.0.0) + rspec-expectations (~> 3.0.0) + rspec-mocks (~> 3.0.0) + rspec-support (~> 3.0.0) + rspec-support (3.0.4) + sprockets (3.3.4) + rack (~> 1.0) + sprockets-rails (2.3.3) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (>= 2.8, < 4.0) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.5) + tzinfo (0.3.44) + xpath (2.0.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + appraisal + capybara + carrierwave + database_cleaner + factory_girl + faker + nocms-pages! + rails (= 4.0.13) + rspec-rails (~> 3.0.0.beta) + sqlite3 diff --git a/gemfiles/rails_4_1.gemfile b/gemfiles/rails_4_1.gemfile new file mode 100644 index 0000000..d4cd06b --- /dev/null +++ b/gemfiles/rails_4_1.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", "4.1.13" + +group :development, :test do + gem "faker" + gem "carrierwave" +end + +group :test do + gem "rspec-rails", "~> 3.0.0.beta" + gem "factory_girl" + gem "capybara" + gem "database_cleaner" + gem "appraisal" +end + +gemspec :path => "../" diff --git a/gemfiles/rails_4_1.gemfile.lock b/gemfiles/rails_4_1.gemfile.lock new file mode 100644 index 0000000..def3561 --- /dev/null +++ b/gemfiles/rails_4_1.gemfile.lock @@ -0,0 +1,143 @@ +PATH + remote: ../ + specs: + nocms-pages (1.0.0) + awesome_nested_set (>= 3.0.0.rc.6) + globalize (>= 4.0.0, < 5.1) + nocms-blocks (~> 1.1.1) + rails (~> 4.0, >= 4.0.3) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.1.13) + actionpack (= 4.1.13) + actionview (= 4.1.13) + mail (~> 2.5, >= 2.5.4) + actionpack (4.1.13) + actionview (= 4.1.13) + activesupport (= 4.1.13) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + actionview (4.1.13) + activesupport (= 4.1.13) + builder (~> 3.1) + erubis (~> 2.7.0) + activemodel (4.1.13) + activesupport (= 4.1.13) + builder (~> 3.1) + activerecord (4.1.13) + activemodel (= 4.1.13) + activesupport (= 4.1.13) + arel (~> 5.0.0) + activesupport (4.1.13) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) + appraisal (2.1.0) + bundler + rake + thor (>= 0.14.0) + arel (5.0.1.20140414130214) + awesome_nested_set (3.0.2) + activerecord (>= 4.0.0, < 5) + builder (3.2.2) + capybara (2.5.0) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) + database_cleaner (1.5.0) + diff-lcs (1.2.5) + erubis (2.7.0) + factory_girl (4.5.0) + activesupport (>= 3.0.0) + faker (1.5.0) + i18n (~> 0.5) + globalize (4.0.3) + activemodel (>= 4.0.0, < 5) + activerecord (>= 4.0.0, < 5) + i18n (0.7.0) + json (1.8.3) + mail (2.6.3) + mime-types (>= 1.16, < 3) + mime-types (2.6.1) + mini_portile (0.6.2) + minitest (5.8.0) + nocms-blocks (1.1.1) + awesome_nested_set (~> 3.0.0) + globalize (>= 4.0.0, < 5.1) + rails (~> 4.0, <= 4.2.3) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) + rack (1.5.5) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.1.13) + actionmailer (= 4.1.13) + actionpack (= 4.1.13) + actionview (= 4.1.13) + activemodel (= 4.1.13) + activerecord (= 4.1.13) + activesupport (= 4.1.13) + bundler (>= 1.3.0, < 2.0) + railties (= 4.1.13) + sprockets-rails (~> 2.0) + railties (4.1.13) + actionpack (= 4.1.13) + activesupport (= 4.1.13) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.4.2) + rspec-core (3.0.4) + rspec-support (~> 3.0.0) + rspec-expectations (3.0.4) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.0.0) + rspec-mocks (3.0.4) + rspec-support (~> 3.0.0) + rspec-rails (3.0.2) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.0.0) + rspec-expectations (~> 3.0.0) + rspec-mocks (~> 3.0.0) + rspec-support (~> 3.0.0) + rspec-support (3.0.4) + sprockets (3.3.4) + rack (~> 1.0) + sprockets-rails (2.3.3) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (>= 2.8, < 4.0) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.5) + tzinfo (1.2.2) + thread_safe (~> 0.1) + xpath (2.0.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + appraisal + capybara + carrierwave + database_cleaner + factory_girl + faker + nocms-pages! + rails (= 4.1.13) + rspec-rails (~> 3.0.0.beta) + sqlite3 diff --git a/gemfiles/rails_4_2.gemfile b/gemfiles/rails_4_2.gemfile new file mode 100644 index 0000000..b6f8ae7 --- /dev/null +++ b/gemfiles/rails_4_2.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", "4.2.3" + +group :development, :test do + gem "faker" + gem "carrierwave" +end + +group :test do + gem "rspec-rails", "~> 3.0.0.beta" + gem "factory_girl" + gem "capybara" + gem "database_cleaner" + gem "appraisal" +end + +gemspec :path => "../" diff --git a/gemfiles/rails_4_2.gemfile.lock b/gemfiles/rails_4_2.gemfile.lock new file mode 100644 index 0000000..777c8b6 --- /dev/null +++ b/gemfiles/rails_4_2.gemfile.lock @@ -0,0 +1,165 @@ +PATH + remote: ../ + specs: + nocms-pages (1.0.0) + awesome_nested_set (>= 3.0.0.rc.6) + globalize (>= 4.0.0, < 5.1) + nocms-blocks (~> 1.1.1) + rails (~> 4.0, >= 4.0.3) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.2.3) + actionpack (= 4.2.3) + actionview (= 4.2.3) + activejob (= 4.2.3) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.3) + actionview (= 4.2.3) + activesupport (= 4.2.3) + rack (~> 1.6) + rack-test (~> 0.6.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (4.2.3) + activesupport (= 4.2.3) + builder (~> 3.1) + erubis (~> 2.7.0) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + activejob (4.2.3) + activesupport (= 4.2.3) + globalid (>= 0.3.0) + activemodel (4.2.3) + activesupport (= 4.2.3) + builder (~> 3.1) + activerecord (4.2.3) + activemodel (= 4.2.3) + activesupport (= 4.2.3) + arel (~> 6.0) + activesupport (4.2.3) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + appraisal (2.1.0) + bundler + rake + thor (>= 0.14.0) + arel (6.0.3) + awesome_nested_set (3.0.2) + activerecord (>= 4.0.0, < 5) + builder (3.2.2) + capybara (2.5.0) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) + database_cleaner (1.5.0) + diff-lcs (1.2.5) + erubis (2.7.0) + factory_girl (4.5.0) + activesupport (>= 3.0.0) + faker (1.5.0) + i18n (~> 0.5) + globalid (0.3.6) + activesupport (>= 4.1.0) + globalize (5.0.1) + activemodel (>= 4.2.0, < 4.3) + activerecord (>= 4.2.0, < 4.3) + i18n (0.7.0) + json (1.8.3) + loofah (2.0.3) + nokogiri (>= 1.5.9) + mail (2.6.3) + mime-types (>= 1.16, < 3) + mime-types (2.6.1) + mini_portile (0.6.2) + minitest (5.8.0) + nocms-blocks (1.1.1) + awesome_nested_set (~> 3.0.0) + globalize (>= 4.0.0, < 5.1) + rails (~> 4.0, <= 4.2.3) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) + rack (1.6.4) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.2.3) + actionmailer (= 4.2.3) + actionpack (= 4.2.3) + actionview (= 4.2.3) + activejob (= 4.2.3) + activemodel (= 4.2.3) + activerecord (= 4.2.3) + activesupport (= 4.2.3) + bundler (>= 1.3.0, < 2.0) + railties (= 4.2.3) + sprockets-rails + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.7) + activesupport (>= 4.2.0.beta, < 5.0) + nokogiri (~> 1.6.0) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.0.2) + loofah (~> 2.0) + railties (4.2.3) + actionpack (= 4.2.3) + activesupport (= 4.2.3) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.4.2) + rspec-core (3.0.4) + rspec-support (~> 3.0.0) + rspec-expectations (3.0.4) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.0.0) + rspec-mocks (3.0.4) + rspec-support (~> 3.0.0) + rspec-rails (3.0.2) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.0.0) + rspec-expectations (~> 3.0.0) + rspec-mocks (~> 3.0.0) + rspec-support (~> 3.0.0) + rspec-support (3.0.4) + sprockets (3.3.4) + rack (~> 1.0) + sprockets-rails (2.3.3) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (>= 2.8, < 4.0) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.5) + tzinfo (1.2.2) + thread_safe (~> 0.1) + xpath (2.0.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + appraisal + capybara + carrierwave + database_cleaner + factory_girl + faker + nocms-pages! + rails (= 4.2.3) + rspec-rails (~> 3.0.0.beta) + sqlite3 diff --git a/lib/generators/nocms/pages_generator.rb b/lib/generators/nocms/pages_generator.rb index cbf8cf8..f7d4abf 100644 --- a/lib/generators/nocms/pages_generator.rb +++ b/lib/generators/nocms/pages_generator.rb @@ -7,6 +7,10 @@ def generate_nocms_pages_initializer template "config/initializers/nocms/pages.rb", File.join(destination_root, "config", "initializers", "nocms", "pages.rb") end + def generate_nocms_blocks_initializer + generate "nocms:blocks" + end + def self.namespace "nocms:pages" end diff --git a/lib/generators/nocms/templates/config/initializers/nocms/pages.rb b/lib/generators/nocms/templates/config/initializers/nocms/pages.rb index dda00cc..d189827 100644 --- a/lib/generators/nocms/templates/config/initializers/nocms/pages.rb +++ b/lib/generators/nocms/templates/config/initializers/nocms/pages.rb @@ -1,45 +1,5 @@ NoCms::Pages.configure do |config| - # Enable Rails fragment cache for the block templates when you call the render_block helper - # You can override this cache setting in any block configuration below or sending - # the cache option true or false when calling the block helpers - # e.g: render_block block, cache: true - # config.cache_enabled = false - - # In this section we configure block layouts. It's just an array of layouts, each consisting on a hash. - # Each layout has a series of options - # E.g: config.block_layouts = { - # 'title-long_text' => { - # template: 'title-long_text', # This is the template of this block, - # # used as a partial both in the front - # # and the admin (if you use the nocms-admin-pages gem) - # fields: { # This is the list of fields a block with this layout would have - # title: :string, - # long_text: :text, - # image: Image, # You may use another ActiveRecord classes of your own - # } - # allow_nested_blocks: true, # A block with this layout may include a list of nested blocks - # # This setting is actually used by nocms-admin-pages gem to show - # # nested forms - # nest_levels: [0] # Some layout may not be nestable, or useful only in certain nesting level - # # Once again, this setting is used by nocms-admin-pages gem to hide certain - # # in nested blocks. When blank, it's assumed there's no restriction. - # cache_enabled: false # When setting cache_enabled you will be **overriding** the global cache_enabled - # # setting. If you don't set a cache setting then it will use the global cache - # # setting specified above - # }, - # 'title-3_columns_text' => { - # template: 'title-3_columns_text', - # fields: { - # title: :string, - # column_1: :text, - # column_2: :text, - # column_3: :text - # } - # } - # } - # config.block_layouts = {} - # 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 # config.use_body = false diff --git a/lib/nocms/pages/engine.rb b/lib/nocms/pages/engine.rb index 28daa9d..3ea3053 100644 --- a/lib/nocms/pages/engine.rb +++ b/lib/nocms/pages/engine.rb @@ -1,10 +1,17 @@ require 'globalize' +require 'nocms-blocks' require 'awesome_nested_set' module NoCms module Pages class Engine < ::Rails::Engine isolate_namespace NoCms::Pages + + config.to_prepare do + Dir.glob(NoCms::Pages::Engine.root + "app/decorators/**/*_decorator*.rb").each do |c| + require_dependency(c) + end + end end end end diff --git a/lib/nocms/pages/version.rb b/lib/nocms/pages/version.rb index 28df6bb..89c0d4e 100644 --- a/lib/nocms/pages/version.rb +++ b/lib/nocms/pages/version.rb @@ -1,5 +1,5 @@ module NoCms module Pages - VERSION = "0.1.0" + VERSION = "1.0.0" end end diff --git a/lib/tasks/pages_tasks.rake b/lib/tasks/pages_tasks.rake index 870794e..84c13b8 100644 --- a/lib/tasks/pages_tasks.rake +++ b/lib/tasks/pages_tasks.rake @@ -1,4 +1,45 @@ -# desc "Explaining what the task does" -# task :pages do -# # Task goes here -# end +namespace :no_cms do + + namespace :pages do + + desc 'Task for migrating blocks from NoCms::Pages::Block model to NoCms::Blocks::Block' + task :migrate_blocks => :environment do + root_blocks = NoCms::Pages::Deprecated::Block.dump + root_blocks.each do |root_block| + import_block root_block + end + end + + def import_block block + return if block[:page].nil? + + # Due to globalize we have to send the default translation via the object attributes + # instead of using the translation_attributes + default_translation = block[:translations][I18n.locale] + other_translations = block[:translations].reject{|locale, _| locale == I18n.locale } + + new_block = block[:page].blocks.build position: block[:position], + translations_attributes: other_translations.map {|locale, translation| + { + locale: locale, + layout: translation[:layout], + draft: translation[:draft], + fields_info: translation[:fields_info].to_hash + } + }, + layout: default_translation[:layout], + draft: default_translation[:draft], + fields_info: default_translation[:fields_info].to_hash + + new_block.translations.each(&:save!) + + new_block.children = block[:children].map{|b| import_block b} + new_block.save! + + block[:page].blocks << new_block + new_block + end + + end + +end diff --git a/pages.gemspec b/pages.gemspec index 8dffaec..c4b623c 100644 --- a/pages.gemspec +++ b/pages.gemspec @@ -16,8 +16,9 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.md"] s.add_dependency "rails", '~> 4.0', '>= 4.0.3' - s.add_dependency "globalize", '~> 4.0', '>= 4.0.0' + s.add_dependency "globalize", '>= 4.0.0', '< 5.1' s.add_dependency "awesome_nested_set", '>= 3.0.0.rc.6' + s.add_dependency "nocms-blocks", '~> 1.1.1' s.add_development_dependency "sqlite3" end diff --git a/spec/dummy/app/views/no_cms/pages/blocks/_logo_caption.html.erb b/spec/dummy/app/views/no_cms/blocks/blocks/_logo_caption.html.erb similarity index 100% rename from spec/dummy/app/views/no_cms/pages/blocks/_logo_caption.html.erb rename to spec/dummy/app/views/no_cms/blocks/blocks/_logo_caption.html.erb diff --git a/spec/dummy/app/views/no_cms/pages/blocks/_title_3_columns.html.erb b/spec/dummy/app/views/no_cms/blocks/blocks/_title_3_columns.html.erb similarity index 100% rename from spec/dummy/app/views/no_cms/pages/blocks/_title_3_columns.html.erb rename to spec/dummy/app/views/no_cms/blocks/blocks/_title_3_columns.html.erb diff --git a/spec/dummy/config/initializers/nocms/blocks.rb b/spec/dummy/config/initializers/nocms/blocks.rb new file mode 100644 index 0000000..482fc16 --- /dev/null +++ b/spec/dummy/config/initializers/nocms/blocks.rb @@ -0,0 +1,77 @@ +NoCms::Blocks.configure do |config| + + # Enable Rails fragment cache for the block templates when you call the render_block helper + # You can override this cache setting in any block configuration below or sending + # the cache option true or false when calling the block helpers + # e.g: render_block block, cache: true + # config.cache_enabled = false + + # In this section we configure block layouts. It's just an array of layouts, each consisting on a hash. + # Each layout has a series of options + # E.g: config.block_layouts = { + # 'title-long_text' => { + # template: 'title-long_text', # This is the template of this block, + # # used as a partial both in the front + # # and the admin (if you use the nocms-admin gem) + # fields: { # This is the list of fields a block with this layout would have + # title: :string, + # long_text: :text, + # image: Image, # You may use another ActiveRecord classes of your own + # column: { # You can configure the block with more options than just + # # the type of the field. If you use the "quick" configuration + # # all other settings will get the default value + # type: :text, # The type of the field, just as explained before + # translated: true # If the field must store different values for + # # each translation. By default every field is + # # translated + # } + # } + # allow_nested_blocks: true, # A block with this layout may include a list of nested blocks + # # This setting is actually used by nocms-admin gem to show + # # nested forms + # nest_levels: [0] # Some layout may not be nestable, or useful only in certain nesting level + # # Once again, this setting is used by nocms-admin gem to hide certain + # # in nested blocks. When blank, it's assumed there's no restriction. + # cache_enabled: false # When setting cache_enabled you will be **overriding** the global cache_enabled + # # setting. If you don't set a cache setting then it will use the global cache + # # setting specified above + # }, + # 'title-3_columns_text' => { + # template: 'title-3_columns_text', + # fields: { + # title: :string, + # column_1: :text, + # column_2: :text, + # column_3: :text + # } + # } + # } + # config.block_layouts = {} + + config.block_layouts = { + 'default' => { + template: 'default', + fields: { + title: :string, + body: :text + } + }, + 'title-3_columns' => { + template: 'title_3_columns', + fields: { + title: :string, + column_1: :text, + column_2: :text, + column_3: :text + }, + }, + 'logo-caption' => { + template: 'logo_caption', + fields: { + caption: :string, + logo: TestImage + } + } + } + +end diff --git a/spec/dummy/config/initializers/nocms/pages.rb b/spec/dummy/config/initializers/nocms/pages.rb index a4fe003..b88b9c4 100644 --- a/spec/dummy/config/initializers/nocms/pages.rb +++ b/spec/dummy/config/initializers/nocms/pages.rb @@ -1,62 +1,9 @@ NoCms::Pages.configure do |config| - # Enable Rails fragment cache for the block templates when you call the render_block helper - # You can override this cache setting in any block configuration below or sending - # the cache option true or false when calling the menu helpers - # e.g: render_block block, cache: true - # config.cache_enabled = false - - # In this section we configure block layouts. It's just an array of layouts, each consisting on a hash. - # Each layout has a template and a list of fields with a type. - # E.g: config.block_layouts = { - # 'title-long_text' => { - # template: 'title-long_text', - # fields: { - # title: :string, - # long_text: :text - # } - # }, - # 'title-3_columns_text' => { - # template: 'title-3_columns_text', - # fields: { - # title: :string, - # column_1: :text, - # column_2: :text, - # column_3: :text - # } - # } - # } - # config.block_layouts = {} - - config.block_layouts = { - 'default' => { - template: 'default', - fields: { - title: :string, - body: :text - } - }, - 'title-3_columns' => { - template: 'title_3_columns', - fields: { - title: :string, - column_1: :text, - column_2: :text, - column_3: :text - }, - }, - 'logo-caption' => { - template: 'logo_caption', - fields: { - caption: :string, - logo: TestImage - } - } - } + # 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 + config.use_body = true # By default we use all the layouts in the app/views/layouts from the app # config.page_layouts = ['application', ...] - config.use_body = true - end diff --git a/spec/dummy/db/migrate/20140227103350_create_no_cms_pages.no_cms_pages.rb b/spec/dummy/db/migrate/20140227103350_create_no_cms_pages.no_cms_pages.rb deleted file mode 100644 index a7f8a32..0000000 --- a/spec/dummy/db/migrate/20140227103350_create_no_cms_pages.no_cms_pages.rb +++ /dev/null @@ -1,15 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140226123742) -class CreateNoCmsPages < ActiveRecord::Migration - def change - create_table :no_cms_pages_pages do |t| - t.timestamps - end - - create_table :no_cms_pages_page_translations do |t| - t.belongs_to :no_cms_pages_page, index: true - t.string :locale - t.string :title - t.string :body - end - end -end diff --git a/spec/dummy/db/migrate/20140227103351_create_no_cms_blocks.no_cms_pages.rb b/spec/dummy/db/migrate/20140227103351_create_no_cms_blocks.no_cms_pages.rb deleted file mode 100644 index 7d9efb6..0000000 --- a/spec/dummy/db/migrate/20140227103351_create_no_cms_blocks.no_cms_pages.rb +++ /dev/null @@ -1,17 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140227100626) -class CreateNoCmsBlocks < ActiveRecord::Migration - def change - create_table :no_cms_pages_blocks do |t| - t.belongs_to :page, index: true - - t.timestamps - end - - create_table :no_cms_pages_block_translations do |t| - t.belongs_to :no_cms_pages_block, index: true - t.string :locale - t.string :layout - end - - end -end diff --git a/spec/dummy/db/migrate/20140227163905_add_fields_info_to_no_cms_block.no_cms_pages.rb b/spec/dummy/db/migrate/20140227163905_add_fields_info_to_no_cms_block.no_cms_pages.rb deleted file mode 100644 index 6fdfd98..0000000 --- a/spec/dummy/db/migrate/20140227163905_add_fields_info_to_no_cms_block.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140227143723) -class AddFieldsInfoToNoCmsBlock < ActiveRecord::Migration - def change - add_column :no_cms_pages_block_translations, :fields_info, :longtext, default: Hash.new.to_yaml - end -end diff --git a/spec/dummy/db/migrate/20140228114240_add_slug_to_no_cms_page.no_cms_pages.rb b/spec/dummy/db/migrate/20140228114240_add_slug_to_no_cms_page.no_cms_pages.rb deleted file mode 100644 index c54e074..0000000 --- a/spec/dummy/db/migrate/20140228114240_add_slug_to_no_cms_page.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140228112643) -class AddSlugToNoCmsPage < ActiveRecord::Migration - def change - add_column :no_cms_pages_page_translations, :slug, :string - end -end diff --git a/spec/dummy/db/migrate/20140303092928_add_awesome_nested_set_to_no_cms_pages.no_cms_pages.rb b/spec/dummy/db/migrate/20140303092928_add_awesome_nested_set_to_no_cms_pages.no_cms_pages.rb deleted file mode 100644 index 9ecb1be..0000000 --- a/spec/dummy/db/migrate/20140303092928_add_awesome_nested_set_to_no_cms_pages.no_cms_pages.rb +++ /dev/null @@ -1,9 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140303092727) -class AddAwesomeNestedSetToNoCmsPages < ActiveRecord::Migration - def change - add_column :no_cms_pages_pages, :parent_id, :integer - add_column :no_cms_pages_pages, :lft, :integer - add_column :no_cms_pages_pages, :rgt, :integer - add_column :no_cms_pages_pages, :depth, :integer - end -end diff --git a/spec/dummy/db/migrate/20140303100954_add_path_to_no_cms_page.no_cms_pages.rb b/spec/dummy/db/migrate/20140303100954_add_path_to_no_cms_page.no_cms_pages.rb deleted file mode 100644 index fe5bc71..0000000 --- a/spec/dummy/db/migrate/20140303100954_add_path_to_no_cms_page.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140303100908) -class AddPathToNoCmsPage < ActiveRecord::Migration - def change - add_column :no_cms_pages_page_translations, :path, :string - end -end diff --git a/spec/dummy/db/migrate/20140303123920_add_poisition_to_no_cms_block.no_cms_pages.rb b/spec/dummy/db/migrate/20140303123920_add_poisition_to_no_cms_block.no_cms_pages.rb deleted file mode 100644 index d489559..0000000 --- a/spec/dummy/db/migrate/20140303123920_add_poisition_to_no_cms_block.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140303123845) -class AddPoisitionToNoCmsBlock < ActiveRecord::Migration - def change - add_column :no_cms_pages_blocks, :position, :integer - end -end diff --git a/spec/dummy/db/migrate/20140303175058_add_template_to_no_cms_page.no_cms_pages.rb b/spec/dummy/db/migrate/20140303175058_add_template_to_no_cms_page.no_cms_pages.rb deleted file mode 100644 index 55dd498..0000000 --- a/spec/dummy/db/migrate/20140303175058_add_template_to_no_cms_page.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140303145615) -class AddTemplateToNoCmsPage < ActiveRecord::Migration - def change - add_column :no_cms_pages_pages, :template, :string - end -end diff --git a/spec/dummy/db/migrate/20140313171429_add_draft_to_block.no_cms_pages.rb b/spec/dummy/db/migrate/20140313171429_add_draft_to_block.no_cms_pages.rb deleted file mode 100644 index 3b44137..0000000 --- a/spec/dummy/db/migrate/20140313171429_add_draft_to_block.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140313171000) -class AddDraftToBlock < ActiveRecord::Migration - def change - add_column :no_cms_pages_block_translations, :draft, :boolean, default: false - end -end diff --git a/spec/dummy/db/migrate/20140314121241_add_draft_to_pages.no_cms_pages.rb b/spec/dummy/db/migrate/20140314121241_add_draft_to_pages.no_cms_pages.rb deleted file mode 100644 index 4222d2c..0000000 --- a/spec/dummy/db/migrate/20140314121241_add_draft_to_pages.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140314110439) -class AddDraftToPages < ActiveRecord::Migration - def change - add_column :no_cms_pages_page_translations, :draft, :boolean, default: false - end -end diff --git a/spec/dummy/db/migrate/20140329161259_add_parent_id_to_no_cms_pages_block.no_cms_pages.rb b/spec/dummy/db/migrate/20140329161259_add_parent_id_to_no_cms_pages_block.no_cms_pages.rb deleted file mode 100644 index a760aa8..0000000 --- a/spec/dummy/db/migrate/20140329161259_add_parent_id_to_no_cms_pages_block.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140329160306) -class AddParentIdToNoCmsPagesBlock < ActiveRecord::Migration - def change - add_reference :no_cms_pages_blocks, :parent, index: true - end -end diff --git a/spec/dummy/db/migrate/20140407083148_add_css_class_and_id_to_no_cms_pages_page_translations.no_cms_pages.rb b/spec/dummy/db/migrate/20140407083148_add_css_class_and_id_to_no_cms_pages_page_translations.no_cms_pages.rb deleted file mode 100644 index e79735c..0000000 --- a/spec/dummy/db/migrate/20140407083148_add_css_class_and_id_to_no_cms_pages_page_translations.no_cms_pages.rb +++ /dev/null @@ -1,7 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140407083115) -class AddCssClassAndIdToNoCmsPagesPageTranslations < ActiveRecord::Migration - def change - add_column :no_cms_pages_page_translations, :css_class, :string - add_column :no_cms_pages_page_translations, :css_id, :string - end -end diff --git a/spec/dummy/db/migrate/20140702114154_add_cache_enabled_to_no_cms_pages_page_translations.no_cms_pages.rb b/spec/dummy/db/migrate/20140702114154_add_cache_enabled_to_no_cms_pages_page_translations.no_cms_pages.rb deleted file mode 100644 index d7a22d6..0000000 --- a/spec/dummy/db/migrate/20140702114154_add_cache_enabled_to_no_cms_pages_page_translations.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140702112813) -class AddCacheEnabledToNoCmsPagesPageTranslations < ActiveRecord::Migration - def change - add_column :no_cms_pages_page_translations, :cache_enabled, :boolean, default: true - end -end diff --git a/spec/dummy/db/migrate/20140806074938_add_layout_to_no_cms_pages_page.no_cms_pages.rb b/spec/dummy/db/migrate/20140806074938_add_layout_to_no_cms_pages_page.no_cms_pages.rb deleted file mode 100644 index 663ea0a..0000000 --- a/spec/dummy/db/migrate/20140806074938_add_layout_to_no_cms_pages_page.no_cms_pages.rb +++ /dev/null @@ -1,6 +0,0 @@ -# This migration comes from no_cms_pages (originally 20140806074811) -class AddLayoutToNoCmsPagesPage < ActiveRecord::Migration - def change - add_column :no_cms_pages_pages, :layout, :string - end -end diff --git a/spec/dummy/db/migrate/20150910163021_create_no_cms_blocks_blocks.no_cms_blocks.rb b/spec/dummy/db/migrate/20150910163021_create_no_cms_blocks_blocks.no_cms_blocks.rb new file mode 100644 index 0000000..3183b1d --- /dev/null +++ b/spec/dummy/db/migrate/20150910163021_create_no_cms_blocks_blocks.no_cms_blocks.rb @@ -0,0 +1,22 @@ +# This migration comes from no_cms_blocks (originally 20140405135410) +# This migration comes from no_cms_blocks (originally 20140405135410) +class CreateNoCmsBlocksBlocks < ActiveRecord::Migration + def change + create_table :no_cms_blocks_blocks do |t| + + t.timestamps + end + + NoCms::Blocks::Block.translated_attribute_names = [:locale, :layout, :fields_info, :draft] + + create_table :no_cms_blocks_block_translations do |t| + t.belongs_to :no_cms_blocks_block + t.string :locale + t.string :layout + t.text :fields_info, :limit => 4294967295 + t.boolean :draft + end + + add_index :no_cms_blocks_block_translations, :no_cms_blocks_block_id, { name: 'no_cms_blocks_blocks_translations_block_id'} + end +end diff --git a/spec/dummy/db/migrate/20150910163022_add_awesome_nested_set_to_no_cms_blocks.no_cms_blocks.rb b/spec/dummy/db/migrate/20150910163022_add_awesome_nested_set_to_no_cms_blocks.no_cms_blocks.rb new file mode 100644 index 0000000..84d1e91 --- /dev/null +++ b/spec/dummy/db/migrate/20150910163022_add_awesome_nested_set_to_no_cms_blocks.no_cms_blocks.rb @@ -0,0 +1,9 @@ +# This migration comes from no_cms_blocks (originally 20140405150944) +class AddAwesomeNestedSetToNoCmsBlocks < ActiveRecord::Migration + def change + add_column :no_cms_blocks_blocks, :parent_id, :integer + add_column :no_cms_blocks_blocks, :lft, :integer + add_column :no_cms_blocks_blocks, :rgt, :integer + add_column :no_cms_blocks_blocks, :depth, :integer + end +end diff --git a/spec/dummy/db/migrate/20150910163023_add_position_to_no_cms_blocks_block.no_cms_blocks.rb b/spec/dummy/db/migrate/20150910163023_add_position_to_no_cms_blocks_block.no_cms_blocks.rb new file mode 100644 index 0000000..3c150a8 --- /dev/null +++ b/spec/dummy/db/migrate/20150910163023_add_position_to_no_cms_blocks_block.no_cms_blocks.rb @@ -0,0 +1,6 @@ +# This migration comes from no_cms_blocks (originally 20140618150651) +class AddPositionToNoCmsBlocksBlock < ActiveRecord::Migration + def change + add_column :no_cms_blocks_blocks, :position, :integer + end +end diff --git a/spec/dummy/db/migrate/20150910163024_add_non_translated_fields_info_to_no_cms_blocks_block.no_cms_blocks.rb b/spec/dummy/db/migrate/20150910163024_add_non_translated_fields_info_to_no_cms_blocks_block.no_cms_blocks.rb new file mode 100644 index 0000000..8a05f8e --- /dev/null +++ b/spec/dummy/db/migrate/20150910163024_add_non_translated_fields_info_to_no_cms_blocks_block.no_cms_blocks.rb @@ -0,0 +1,6 @@ +# This migration comes from no_cms_blocks (originally 20150709132202) +class AddNonTranslatedFieldsInfoToNoCmsBlocksBlock < ActiveRecord::Migration + def change + add_column :no_cms_blocks_blocks, :fields_info, :longtext + end +end diff --git a/spec/dummy/db/migrate/20150910163025_move_layout_from_no_cms_blocks_block_translations_to_no_cms_blocks_blocks.no_cms_blocks.rb b/spec/dummy/db/migrate/20150910163025_move_layout_from_no_cms_blocks_block_translations_to_no_cms_blocks_blocks.no_cms_blocks.rb new file mode 100644 index 0000000..be97232 --- /dev/null +++ b/spec/dummy/db/migrate/20150910163025_move_layout_from_no_cms_blocks_block_translations_to_no_cms_blocks_blocks.no_cms_blocks.rb @@ -0,0 +1,20 @@ +# This migration comes from no_cms_blocks (originally 20150710112549) +class MoveLayoutFromNoCmsBlocksBlockTranslationsToNoCmsBlocksBlocks < ActiveRecord::Migration + def up + add_column :no_cms_blocks_blocks, :layout, :string + + NoCms::Blocks::Block::Translation.all.each do |translation| + translation.globalized_model.update_column layout: translation.layout + end + + remove_column :no_cms_blocks_block_translations, :layout, :string + end + + def down + add_column :no_cms_blocks_block_translations, :layout, :string + NoCms::Blocks::Block::Translation.all.each do |translation| + translation.update_column layout: translation.globalized_model.layout + end + remove_column :no_cms_blocks_blocks, :layout, :string + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 55c5ff8..f87292f 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -11,28 +11,45 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140806074938) do +ActiveRecord::Schema.define(version: 20150910163025) do - create_table "no_cms_pages_block_translations", force: true do |t| - t.integer "no_cms_pages_block_id" + create_table "no_cms_blocks_block_translations", force: true do |t| + t.integer "no_cms_blocks_block_id" t.string "locale" - t.string "layout" - t.text "fields_info", default: "--- {}\n" - t.boolean "draft", default: false + t.text "fields_info", limit: 4294967295 + t.boolean "draft" end - add_index "no_cms_pages_block_translations", ["no_cms_pages_block_id"], name: "index_no_cms_pages_block_translations_on_no_cms_pages_block_id" + add_index "no_cms_blocks_block_translations", ["no_cms_blocks_block_id"], name: "no_cms_blocks_blocks_translations_block_id" - create_table "no_cms_pages_blocks", force: true do |t| - t.integer "page_id" + create_table "no_cms_blocks_blocks", force: true do |t| t.datetime "created_at" t.datetime "updated_at" - t.integer "position" t.integer "parent_id" + t.integer "lft" + t.integer "rgt" + t.integer "depth" + t.integer "position" + t.text "fields_info" + t.string "layout" end - add_index "no_cms_pages_blocks", ["page_id"], name: "index_no_cms_pages_blocks_on_page_id" - add_index "no_cms_pages_blocks", ["parent_id"], name: "index_no_cms_pages_blocks_on_parent_id" + create_table "no_cms_blocks_blocks_pages_pages", id: false, force: true do |t| + t.integer "page_id" + t.integer "block_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "no_cms_pages_block_translations", force: true do |t| + t.integer "no_cms_pages_block_id" + t.string "locale" + t.string "layout" + t.text "fields_info" + t.boolean "draft", default: false + end + + add_index "no_cms_pages_block_translations", ["no_cms_pages_block_id"], name: "index_no_cms_pages_block_translations_on_no_cms_pages_block_id" create_table "no_cms_pages_page_translations", force: true do |t| t.integer "no_cms_pages_page_id" diff --git a/spec/dummy/db/seeds.rb b/spec/dummy/db/seeds.rb index 7f60cdf..c08b281 100644 --- a/spec/dummy/db/seeds.rb +++ b/spec/dummy/db/seeds.rb @@ -1,5 +1,5 @@ NoCms::Pages::Page.delete_all -NoCms::Pages::Block.delete_all +NoCms::Blocks::Block.delete_all NoCms::Pages::Page.create!( title: Faker::Lorem.sentence, diff --git a/spec/factories/nocms/pages/block.rb b/spec/factories/nocms/pages/block.rb deleted file mode 100644 index 3d972d0..0000000 --- a/spec/factories/nocms/pages/block.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Read about factories at https://github.com/thoughtbot/factory_girl - -FactoryGirl.define do - factory :nocms_block, class: NoCms::Pages::Block do - layout { 'default' } - page { FactoryGirl.create :nocms_page } - end -end diff --git a/spec/models/nocms/pages/block_spec.rb b/spec/models/nocms/pages/block_spec.rb index b51bdae..d91c601 100644 --- a/spec/models/nocms/pages/block_spec.rb +++ b/spec/models/nocms/pages/block_spec.rb @@ -1,15 +1,13 @@ require 'spec_helper' -describe NoCms::Pages::Block do - it_behaves_like "model with required attributes", :nocms_block, [:layout, :page] - it_behaves_like "model with has many relationship", :nocms_block, :nocms_block, :children, :parent +describe NoCms::Blocks::Block do context "when blocks have layouts" do context "with simple fields" do before do - NoCms::Pages.configure do |config| + NoCms::Blocks.configure do |config| config.block_layouts = { 'title-long_text' => { template: 'title-long_text', @@ -22,70 +20,25 @@ end end - let(:block_with_layout) { NoCms::Pages::Block.create attributes_for(:nocms_block).merge(layout: 'title-long_text', title: block_title) } + let(:block_with_layout) { NoCms::Blocks::Block.create attributes_for(:block).merge(layout: 'title-long_text', title: block_title) } let(:block_title) { Faker::Lorem.sentence } + let(:page) { create :nocms_page } subject { block_with_layout } - it("should respond to layout fields") do - expect{subject.title}.to_not raise_error - expect{subject.body}.to_not raise_error - end - - it("should not respond to fields from other layouts") do - expect{subject.no_title}.to raise_error - end - - it("should save info in layout fields") do - expect(subject.title).to eq block_title - end - - it("should save nil fields") do - expect(subject.body).to be_blank - end - - context "when updating various fields" do - - let(:new_block_title) { "new #{Faker::Lorem.sentence}" } - let(:block_body) { Faker::Lorem.paragraph } - - before do - subject.update_attributes title: new_block_title, body: block_body - end - - it("should save info in layout fields") do - expect(subject.title).to eq new_block_title - expect(subject.body).to eq block_body - end - - end - - context "when updating just one field" do - - let(:new_block_title) { "new #{Faker::Lorem.sentence}" } - - before do - subject.update_attribute :title, new_block_title - end - - it("should save info in layout field") do - expect(subject.title).to eq new_block_title - end - - end - context "when updating one field of the block through the page" do let(:new_block_title) { "new #{Faker::Lorem.sentence}" } let(:new_page_title) { "new page #{Faker::Lorem.sentence}" } before do - subject.page.update_attributes! title: new_page_title, blocks_attributes: { "0" => { title: new_block_title, id: subject.id } } + page.blocks << subject + page.update_attributes! title: new_page_title, blocks_attributes: { "0" => { title: new_block_title, id: subject.id } } end it("should save info in layout field") do subject.reload - expect(subject.page.title).to eq new_page_title + expect(page.title).to eq new_page_title expect(subject.title).to eq new_block_title end @@ -99,12 +52,13 @@ let(:new_page_title) { "new page #{Faker::Lorem.sentence}" } before do - subject.page.update_attributes! title: new_page_title, blocks_attributes: { "0" => { title: new_block_title, body: new_block_body, id: subject.id } } + page.blocks << subject + page.update_attributes! title: new_page_title, blocks_attributes: { "0" => { title: new_block_title, body: new_block_body, id: subject.id } } end it("should save info in layout field") do subject.reload - expect(subject.page.title).to eq new_page_title + expect(page.title).to eq new_page_title expect(subject.title).to eq new_block_title expect(subject.body).to eq new_block_body end @@ -113,154 +67,6 @@ end - context "with related models" do - - before do - NoCms::Pages.configure do |config| - config.block_layouts = { - 'logo-caption' => { - template: 'logo_caption', - fields: { - caption: :string, - logo: TestImage - } - } - } - end - end - - let(:image_attributes) { attributes_for(:test_image) } - - let(:block_with_layout) { NoCms::Pages::Block.create attributes_for(:nocms_block).merge( - layout: 'logo-caption', - caption: Faker::Lorem.sentence, - logo: image_attributes - ) - } - before { subject } - subject { block_with_layout } - - it("should respond to layout fields") do - expect{subject.caption}.to_not raise_error - expect{subject.logo}.to_not raise_error - end - - it("should return objects") do - expect(subject.logo).to be_a(TestImage) - end - - it("should return objects with the right value") do - expect(subject.logo.name).to eq image_attributes[:name] - end - - it("should save related objects") do - expect(TestImage.first).to_not be_nil - end - - context "when related objects are modified outside" do - - let(:logo) { TestImage.first } - let(:new_testing_name) { "new testing name" } - - before do - subject - logo.update_attribute :name, new_testing_name - end - - it("should get those modifications") do - expect(subject.reload.logo.name).to eq new_testing_name - end - - it("should not overwrite those modifications") do - subject.save! - expect(logo.reload.name).to eq new_testing_name - end - - end - - context "when we update the related object" do - - let(:logo) { TestImage.first } - let(:new_testing_name) { "new testing name" } - let(:new_image_attributes) { attributes_for(:test_image,:second_image) } - - before do - subject.update_attributes logo: { name: new_testing_name, logo: new_image_attributes[:logo] } - end - - it("should be modified in database") do - expect(logo.name).to eq new_testing_name - end - - it("should modify uploaded files") do - expect(logo[:logo]).to eq File.basename(new_image_attributes[:logo].path) - end - - end - - context "when the related object is not valid" do - - let(:logo) { TestImage.first } - let(:new_testing_caption) { "new testing caption" } - - before do - subject.update_attributes caption: new_testing_caption, logo: { name: nil } - end - - it("should modify the block attribute") do - expect(subject.reload.caption).to eq new_testing_caption - end - - it("should not modify the invalid objects") do - expect(subject.logo.name).to_not eq logo.name - end - - end - - - end - - end - - context "when asigning blocks to pages" do - - let(:page) { create :nocms_page } - let(:block_1) { create :nocms_block, page: page } - let(:block_2) { create :nocms_block, page: page } - - it "blocks positions should be correctly assigned" do - expect(block_1.position).to eq 1 - expect(block_2.position).to eq 2 - end - - context "when having assigned a different position to a block" do - - let(:last_block) { create :nocms_block, page: page } - - before do - block_1 - block_2.update_attribute :position, 10 - end - - it "new block should be the last one" do - expect(last_block.position).to eq 11 - end - - end - - end - - context "when blocks are marked as draft" do - - let(:no_draft_blocks) { create_list :nocms_block, 2, draft: false } - let(:draft_blocks) { create_list :nocms_block, 2, draft: true } - - before { draft_blocks && no_draft_blocks } - - it("should distinguish between drafts and no drafts") do - expect(NoCms::Pages::Block.drafts).to match_array draft_blocks - expect(NoCms::Pages::Block.no_drafts).to match_array no_draft_blocks - end end end diff --git a/spec/models/nocms/pages/page_spec.rb b/spec/models/nocms/pages/page_spec.rb index b210c27..34dfe5d 100644 --- a/spec/models/nocms/pages/page_spec.rb +++ b/spec/models/nocms/pages/page_spec.rb @@ -2,7 +2,6 @@ describe NoCms::Pages::Page do it_behaves_like "model with required attributes", :nocms_page, [:title, :body] - it_behaves_like "model with has many relationship", :nocms_page, :nocms_block, :blocks, :page it_behaves_like "model with has many relationship", :nocms_page, :nocms_page, :children, :parent context "when creating" do @@ -62,9 +61,11 @@ let(:page) { create :nocms_page} let(:duplicated_page) { build :nocms_page, slug: page.slug} + before { duplicated_page.valid? } + subject { duplicated_page } - it("should not have a valid path") { expect(subject.errors_on(:path)).to_not be_blank } + it("should not have a valid path") { expect(subject.errors[:path]).to_not be_blank } end diff --git a/spec/requests/no_cms/pages/pages_spec.rb b/spec/requests/no_cms/pages/pages_spec.rb index 7c6e744..1970448 100644 --- a/spec/requests/no_cms/pages/pages_spec.rb +++ b/spec/requests/no_cms/pages/pages_spec.rb @@ -6,17 +6,17 @@ let(:cms_page) { create :nocms_page } let(:image_attributes) { attributes_for(:test_image) } - let(:block_default_layout) { create :nocms_block, layout: 'default', page: cms_page, title: Faker::Lorem.sentence, body: Faker::Lorem.paragraph } - let(:block_3_columns_layout) { create :nocms_block, layout: 'title-3_columns', page: cms_page, title: Faker::Lorem.sentence, column_1: Faker::Lorem.paragraph, column_2: Faker::Lorem.paragraph, column_3: Faker::Lorem.paragraph } - let(:block_logo) { create :nocms_block, layout: 'logo-caption', page: cms_page, caption: Faker::Lorem.sentence, logo: image_attributes } - let(:block_draft) { create :nocms_block, layout: 'default', page: cms_page, title: Faker::Lorem.sentence, body: Faker::Lorem.paragraph, draft: true } + let(:block_default_layout) { create :block, layout: 'default', title: Faker::Lorem.sentence, body: Faker::Lorem.paragraph } + let(:block_3_columns_layout) { create :block, layout: 'title-3_columns', title: Faker::Lorem.sentence, column_1: Faker::Lorem.paragraph, column_2: Faker::Lorem.paragraph, column_3: Faker::Lorem.paragraph } + let(:block_logo) { create :block, layout: 'logo-caption', caption: Faker::Lorem.sentence, logo: image_attributes } + let(:block_draft) { create :block, layout: 'default', title: Faker::Lorem.sentence, body: Faker::Lorem.paragraph, draft: true } - let(:page_blocks) { [block_default_layout , block_3_columns_layout, block_logo, block_draft] } + let(:page_blocks) { [block_default_layout , block_3_columns_layout, block_logo, block_draft] } context "if page exists" do before do - NoCms::Pages.configure do |config| + NoCms::Blocks.configure do |config| config.block_layouts = { 'default' => { template: 'default', @@ -45,7 +45,7 @@ end - page_blocks + cms_page.blocks = page_blocks visit cms_page.path end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3b67a4c..0e7d866 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,8 @@ # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("../support/**/*.rb")].each { |f| require f } +FactoryGirl.definition_file_paths << "#{Gem::Specification.find_by_name("nocms-blocks").gem_dir}/spec/factories/no_cms/blocks/" + # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. @@ -18,7 +20,7 @@ config.include FactoryGirl::Syntax::Methods # Capybara DSL only in request specs - config.include Capybara::DSL, :type => :request + config.include Capybara::DSL # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" diff --git a/spec/support/concerns/model_with_required_attributes.rb b/spec/support/concerns/model_with_required_attributes.rb index e89c092..432e786 100644 --- a/spec/support/concerns/model_with_required_attributes.rb +++ b/spec/support/concerns/model_with_required_attributes.rb @@ -12,7 +12,7 @@ subject { model_object } it { should_not be_valid } - it { expect(subject.error_on(attribute_name)).to include I18n.t('errors.messages.blank') } + it { expect(subject.errors[attribute_name]).to include I18n.t('errors.messages.blank') } end end