Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
start in on migrations for styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns authored and Jon Yurek committed Mar 8, 2013
1 parent 4a625e0 commit 44a1c6b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
53 changes: 53 additions & 0 deletions features/regenerating_styles.feature
@@ -0,0 +1,53 @@
Feature: Regenerating styles

Scenario: Adding a new style
Given I generate a new rails application
And I have made a simple avatar on the user model
And I start the rails application
And I upload an avatar to the user model
When I add the following style to the user avatar:
"""
large: '124x124#'
"""
And I change the user show page to show the large avatar
Then I see a missing large avatar on the user show page
When I generate the "add_large_thumbnail_to_user_avatar" migration as follows:
"""
def up
add_style :users, :avatar, large: '124x124#'
end
def down
remove_style :users, :avatar, :large
end
"""
And I run the up database migration
Then I see the large avatar on the user show page
When I run the down database migration
Then I see a missing large avatar on the user show page

Scenario: Changing an existing style
Given I generate a new rails application
And I have made the following avatar style on the user model:
"""
thumbnail: '32x32'
"""
And I upload an avatar to the user model
When I change the avatar style on the user model to:
"""
thumbnail: '16x16'
"""
Then I see a "32x32" thumbnail avatar on the user show page
When I generate the "change_user_avatar_thumbnail_size" migration
And the "up" migration for "change_user_avatar_thumbnail_size" is:
"""
change_style :users, :avatar, thumbnail: '16x16'
"""
And the "down" migration for "change_user_avatar_thumbnail_size" is:
"""
change_style :users, :avatar, thumbnail: '32x32'
"""
And I run the up database migration
Then I see a "16x16" thumbnail avatar on the user show page
When I run the down database migration
Then I see a "32x32" thumbnail avatar on the user show page
88 changes: 88 additions & 0 deletions features/step_definitions/mvc_steps.rb
@@ -0,0 +1,88 @@
Given /^I have made a simple avatar on the user model$/ do
run_simple(%{bundle exec #{generator_command} scaffold user})
run_simple(%{bundle exec #{generator_command} paperclip user avatar})
run_simple(%{bundle exec rake db:migrate})
write_file('app/views/users/new.html.erb', <<-VIEW)
<%= form_for @user, :html => { :multipart => true } do |f| %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= submit_tag 'Submit' %>
<% end %>
VIEW
write_file('app/views/users/show.html.erb', <<-VIEW)
<p>Attachment: <%= image_tag @user.avatar.url(:thumbnail) %></p>
VIEW
write_file('app/models/user.rb', <<-MODEL)
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { thumbnail: '8x8#' }
attr_accessible :avatar
end
MODEL
end

Given /^I upload an avatar to the user model$/ do
visit '/users/new'
attach_file('Avatar', File.expand_path('test/fixtures/5k.png'))
click_button 'Submit'
end

When /^I add the following style to the user avatar:$/ do |string|
write_file('app/models/user.rb', <<-MODEL)
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { thumbnail: '8x8#', #{string} }
attr_accessible :avatar
end
MODEL
end

When /^I change the user show page to show the large avatar$/ do
write_file('app/views/users/show.html.erb', <<-VIEW)
<p>Attachment: <%= image_tag @user.avatar.url(:large) %></p>
VIEW
end

Then /^I see a missing large avatar on the user show page$/ do
user = User.last
user.should_not be_nil
visit "/users/#{user.to_param}"

page.source =~ %r{img alt="5k" src="([^"]+)?.*"}
image_path = $1
image_path.should_not be_blank

File.should_not be_exist(Rails.root.join('public',image_path))
end

When /^I generate the "(.*?)" migration as follows:$/ do |migration_name, code|
run_simple(%{bundle exec ./script/rails generate migration #{migration_name}})
migration_filename = Dir[Rails.root.join('db', 'migrate', "*#{migration_name}.rb")].first
write_file(migration_filename, <<-MIGRATION)
class #{migration_name.classify} < ActiveRecord::Migration
#{code}
end
MIGRATION
end

When /^I run the up database migration$/ do
run_simple('bundle exec rake db:migrate')
end

When /^I run the down database migration$/ do
migration_filename = Dir[Rails.root.join('db', 'migrate', '*')].last
migration_filename =~ %r{.*/(\d+)_[^/]+.rb}
version = $1
version.should_not be_blank
run_simple("bundle exec rake db:migrate:down VERSION=#{version}")
end

Then /^I see the large avatar on the user show page$/ do
user = User.last
user.should_not be_nil
visit "/users/#{user.to_param}"

page.source =~ %r{img alt="5k" src="([^"]+)?.*"}
image_path = $1
image_path.should_not be_blank

File.should be_exist(Rails.root.join('public',image_path))
end
18 changes: 16 additions & 2 deletions test/schema_test.rb
Expand Up @@ -64,7 +64,7 @@ def teardown
Dummy.connection.create_table :dummies, :force => true
end

context "migrating up" do
context '#add_attachment' do
context "with single attachment" do
setup do
Dummy.connection.add_attachment :dummies, :avatar
Expand Down Expand Up @@ -111,7 +111,7 @@ def teardown
end
end

context "migrating down" do
context "#remove_attachment" do
setup do
Dummy.connection.change_table :dummies do |t|
t.column :avatar_file_name, :string
Expand Down Expand Up @@ -196,5 +196,19 @@ def teardown
end
end
end

context '#add_style' do
should 'process the specific style'

should 'raise if the style is missing' do
assert_raise ArgumentError do
Dummy.connection.add_style :dummies, :avatar, missing_style: '24x24'
end
end

should 'raise if the attachment is missing'

should 'raise if the model is missing'
end
end
end

0 comments on commit 44a1c6b

Please sign in to comment.