Skip to content

Commit

Permalink
Fixed failing tests, documented wymeditor support.
Browse files Browse the repository at this point in the history
Conflicts:
	app/decorators/controllers/refinery/admin/pages_controller_decorator.rb
	refinerycms-page-images.gemspec
  • Loading branch information
parndt committed Oct 11, 2014
1 parent cfe5e49 commit 5a4e8dd
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 92 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/refinery/image_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/refinery/page_images.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'refinerycms-core'
require 'globalize'

module Refinery
autoload :PageImagesGenerator, 'generators/refinery/page_images_generator'
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion refinerycms-page-images.gemspec
Original file line number Diff line number Diff line change
@@ -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{[email protected]}
Expand All @@ -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
4 changes: 2 additions & 2 deletions spec/factories/page-images.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions spec/models/refinery/blog_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
120 changes: 55 additions & 65 deletions spec/models/refinery/page_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5a4e8dd

Please sign in to comment.