diff --git a/Gemfile b/Gemfile index 216d189..ae47e80 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gemspec gem 'refinerycms', github: 'refinery/refinerycms' gem 'refinerycms-i18n', github: 'refinery/refinerycms-i18n' +gem 'refinerycms-wymeditor', '~> 1.0.0' gem "mime-types", "~> 1.25" diff --git a/app/decorators/controllers/refinery/admin/pages_controller_decorator.rb b/app/decorators/controllers/refinery/admin/pages_controller_decorator.rb index b73becb..f49f2b2 100644 --- a/app/decorators/controllers/refinery/admin/pages_controller_decorator.rb +++ b/app/decorators/controllers/refinery/admin/pages_controller_decorator.rb @@ -1,20 +1,22 @@ Refinery::Admin::PagesController.class_eval do - # We need to add :images_attributes to page_params as it is ignored by strong parameters. (See #100) - def page_params_with_page_image_params + # work around an issue with stack level too deep, due to an issue with decorators. + if self.instance_methods.exclude?(:page_params_with_page_image_params) + # We need to add :images_attributes to page_params as it is ignored by strong parameters. (See #100) + def page_params_with_page_image_params - # Get the :images_attributes hash from params - page_image_params = params.require(:page).permit(images_attributes: [:id, :caption]) + # Get the :images_attributes hash from params + page_image_params = params.require(:page).permit(images_attributes: [:id, :caption]) - # If there is no :images_attributes hash use a blank hash (so it removes deleted images) - page_image_params = {images_attributes:{}} if page_image_params[:images_attributes].nil? + # If there is no :images_attributes hash use a blank hash (so it removes deleted images) + page_image_params = {images_attributes:{}} if page_image_params[:images_attributes].nil? - # Add the :images_attributes hash to the default page_params hash - page_params_without_page_image_params.merge(page_image_params) + # Add the :images_attributes hash to the default page_params hash + page_params_without_page_image_params.merge(page_image_params) - end - - # Swap out the default page_params method with our new one - alias_method_chain :page_params, :page_image_params + end + # Swap out the default page_params method with our new one + alias_method_chain :page_params, :page_image_params + end end diff --git a/app/models/refinery/image_page.rb b/app/models/refinery/image_page.rb index 032c436..84f8969 100644 --- a/app/models/refinery/image_page.rb +++ b/app/models/refinery/image_page.rb @@ -4,6 +4,6 @@ class ImagePage < Refinery::Core::BaseModel belongs_to :image belongs_to :page, :polymorphic => true - translates :caption if self.respond_to?(:translates) + translates :caption end end diff --git a/lib/refinery/page_images.rb b/lib/refinery/page_images.rb index 19d0c97..78c7a19 100644 --- a/lib/refinery/page_images.rb +++ b/lib/refinery/page_images.rb @@ -1,4 +1,5 @@ require 'refinerycms-core' +require 'globalize' module Refinery autoload :PageImagesGenerator, 'generators/refinery/page_images_generator' diff --git a/readme.md b/readme.md index d65e00f..8847f12 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ Page Images allows you to relate one or more images to any page in Refinery whic ## Requirements -* refinerycms >= 2.1.0 +* refinerycms >= 3.0.0 ## Features @@ -18,10 +18,10 @@ Page Images allows you to relate one or more images to any page in Refinery whic ## Install -Add this line to your applications `Gemfile` +Add this line to your application's `Gemfile` ```ruby -gem 'refinerycms-page-images', '~> 2.1.0' +gem 'refinerycms-page-images', '~> 3.0.0' ``` Next run @@ -67,6 +67,12 @@ By default, captions are WYM editors. Prefer `textarea`s? Gotcha : Refinery::PageImages.wysiwyg = false ``` +Note that WYMeditor support requires that you have the extension in your Gemfile: + +```ruby +gem 'refinerycms-wymeditor', '~> 1.0.0' +``` + ## Usage `app/views/refinery/pages/show.html.erb` diff --git a/refinerycms-page-images.gemspec b/refinerycms-page-images.gemspec index 7db818d..1310492 100644 --- a/refinerycms-page-images.gemspec +++ b/refinerycms-page-images.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = %q{refinerycms-page-images} - s.version = %q{3.0.0.dev} + s.version = %q{3.0.0} s.description = %q{Attach images to pages ins Refinery CMS} s.summary = %q{Page Images extension for Refinery CMS} s.email = %q{info@refinerycms.com} @@ -14,4 +14,5 @@ Gem::Specification.new do |s| s.add_dependency 'refinerycms-pages', '~> 3.0.0' s.add_dependency 'decorators', '~> 1.0.0' + s.add_dependency 'globalize', '~> 4.0' end diff --git a/spec/factories/page-images.rb b/spec/factories/page-images.rb index b075e80..5582217 100644 --- a/spec/factories/page-images.rb +++ b/spec/factories/page-images.rb @@ -1,9 +1,9 @@ FactoryGirl.define do factory :page_with_image, :parent => :page do - after(:create) { |p| p.images << FactoryGirl.create(:image) } + after(:create) { |p| p.image_pages.create(image: FactoryGirl.create(:image)) } end factory :blog_post_with_image, :parent => :blog_post do - after(:create) { |b| b.images << FactoryGirl.create(:image) } + after(:create) { |b| b.image_pages.create(image: FactoryGirl.create(:image)) } end if defined? Refinery::Blog::Post end diff --git a/spec/models/refinery/blog_spec.rb b/spec/models/refinery/blog_spec.rb index 3b9bb69..babceeb 100644 --- a/spec/models/refinery/blog_spec.rb +++ b/spec/models/refinery/blog_spec.rb @@ -1,15 +1,17 @@ require 'spec_helper' module Refinery - describe BlogPost do - it "should not have images" do - blog = FactoryGirl.create(:blog_post) - blog.images.count.should == 0 - end + module Blog + describe Post, type: :model do + it "should not have images" do + blog = FactoryGirl.create(:blog_post) + blog.images.count.should == 0 + end - it "should have images" do - blog = FactoryGirl.create(:blog_post_with_image) - blog.images.count.should == 1 + it "should have images" do + blog = FactoryGirl.create(:blog_post_with_image) + blog.images.count.should == 1 + end end end end if defined?(Refinery::Blog::Post) diff --git a/spec/models/refinery/page_spec.rb b/spec/models/refinery/page_spec.rb index 565066c..31e7759 100644 --- a/spec/models/refinery/page_spec.rb +++ b/spec/models/refinery/page_spec.rb @@ -1,29 +1,25 @@ require 'spec_helper' module Refinery - describe Page do + describe Page, type: :model do + let!(:page) { FactoryGirl.create :page } it "can have images added" do - page = FactoryGirl.create(:page) - page.images.count.should eq(0) - - page.images << FactoryGirl.create(:image) - page.images.count.should eq(1) + expect { + page.image_pages.create(image: FactoryGirl.build(:image)) + }.to change{page.images.count}.from(0).to(1) end describe '#images_with_captions' do it 'returns an images_with_captions collection' do - page = FactoryGirl.create(:page) - page.images_with_captions.count.should eq(0) - - page.images << FactoryGirl.create(:image) - page.images_with_captions.count.should eq(1) + expect { + page.image_pages.create(image: FactoryGirl.build(:image)) + }.to change{page.images_with_captions.count}.from(0).to(1) end it 'returns an image and a caption' do - page = FactoryGirl.create(:page) page.images_with_captions.count.should eq(0) - page.images << FactoryGirl.create(:image) + page.image_pages.create(image: FactoryGirl.build(:image)) page.images_with_captions.first[:image].should be_a(Refinery::Image) page.images_with_captions.first[:caption].should be_a(String) end @@ -32,67 +28,61 @@ module Refinery describe "#images_attributes=" do it "adds images" do - page = FactoryGirl.create(:page) image = FactoryGirl.create(:image) - page.images.count.should == 0 - page.update_attributes({:images_attributes => {"0" => {"id" => image.id}}}) - - page.images.count.should == 1 + expect { + page.update_attributes({:images_attributes => {"0" => {"id" => image.id}}}) + }.to change { page.images.count }.from(0).to(1) end - it "deletes specific images" do - page = FactoryGirl.create(:page) - images = [FactoryGirl.create(:image), FactoryGirl.create(:image)] - page.images = images - - page_image_to_keep = page.image_pages.find do |image_page| - image_page.image_id == images.first.id + context 'with images' do + let!(:images) { [FactoryGirl.create(:image), FactoryGirl.create(:image)] } + before do + images.each do |image| + page.image_pages.create(image: image) + end + end + it "deletes specific images" do + page_image_to_keep = page.image_pages.find do |image_page| + image_page.image_id == images.first.id + end + page.update_attributes(:images_attributes => { + "0" => { + "id" => page_image_to_keep.image_id.to_s, + "image_page_id" => page_image_to_keep.id + }, + }) + + page.images.should eq([images.first]) end - page.update_attributes(:images_attributes => { - "0" => { - "id" => page_image_to_keep.image_id.to_s, - "image_page_id" => page_image_to_keep.id - }, - }) - - page.images.should eq([images.first]) - end - - it "deletes all images" do - page = FactoryGirl.create(:page) - images = [FactoryGirl.create(:image), FactoryGirl.create(:image)] - page.images = images - - page.update_attributes(:images_attributes => {"0" => {"id"=>""}}) - - page.images.should be_empty - end - it "reorders images" do - page = FactoryGirl.create(:page) - images = [FactoryGirl.create(:image), FactoryGirl.create(:image)] - page.images = images + it "deletes all images" do + page.update_attributes(:images_attributes => {"0" => {"id"=>""}}) - first_page_image = page.image_pages.find do |image_page| - image_page.image_id == images.first.id + page.images.should be_empty end - second_page_image = page.image_pages.find do |image_page| - image_page.image_id == images.second.id - end - - page.update_attributes(:images_attributes => { - "0" => { - "id" => second_page_image.image_id, - "image_page_id" => second_page_image.id, - }, - "1" => { - "id" => first_page_image.image_id, - "image_page_id" => first_page_image.id, - }, - }) - page.images.should eq([images.second, images.first]) + it "reorders images" do + first_page_image = page.image_pages.find do |image_page| + image_page.image_id == images.first.id + end + second_page_image = page.image_pages.find do |image_page| + image_page.image_id == images.second.id + end + + page.update_attributes(:images_attributes => { + "0" => { + "id" => second_page_image.image_id, + "image_page_id" => second_page_image.id, + }, + "1" => { + "id" => first_page_image.image_id, + "image_page_id" => first_page_image.id, + }, + }) + + page.images.should eq([images.second, images.first]) + end end end end