Commit 5232b192 by Prem Sichanugrist

Remove Rails 2 support from Paperclip

parent 19aedbcb
...@@ -8,7 +8,6 @@ before_script: "sudo ntpdate -ub ntp.ubuntu.com pool.ntp.org; true" ...@@ -8,7 +8,6 @@ before_script: "sudo ntpdate -ub ntp.ubuntu.com pool.ntp.org; true"
script: "bundle exec rake clean test cucumber" script: "bundle exec rake clean test cucumber"
gemfile: gemfile:
- gemfiles/rails2.gemfile - gemfiles/3.0.gemfile
- gemfiles/rails3.gemfile - gemfiles/3.1.gemfile
- gemfiles/rails3_1.gemfile - gemfiles/3.2.gemfile
- gemfiles/rails3_2.gemfile
appraise "rails2" do appraise "3.0" do
gem "rails", "~> 2.3.14" gem "rails", "~> 3.0.12"
gem "paperclip", :path => "../" gem "paperclip", :path => "../"
end end
appraise "rails3" do appraise "3.1" do
gem "rails", "~> 3.0.10" gem "rails", "~> 3.1.4"
gem "paperclip", :path => "../" gem "paperclip", :path => "../"
end end
appraise "rails3_1" do appraise "3.2" do
gem "rails", "~> 3.1.0" gem "rails", "~> 3.2.2"
gem "paperclip", :path => "../" gem "paperclip", :path => "../"
end end
appraise "rails3_2" do
gem "rails", "~> 3.2.0"
gem "paperclip", :path => "../"
end
...@@ -5,7 +5,6 @@ Feature: Rake tasks ...@@ -5,7 +5,6 @@ Feature: Rake tasks
And I run a rails generator to generate a "User" scaffold with "name:string" And I run a rails generator to generate a "User" scaffold with "name:string"
And I run a paperclip generator to add a paperclip "attachment" to the "User" model And I run a paperclip generator to add a paperclip "attachment" to the "User" model
And I run a migration And I run a migration
And I add the paperclip rake task to a Rails 2.3 application
And I add this snippet to the User model: And I add this snippet to the User model:
""" """
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename" has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
......
...@@ -31,33 +31,18 @@ Given /^I run a migration$/ do ...@@ -31,33 +31,18 @@ Given /^I run a migration$/ do
end end
Given /^I update my new user view to include the file upload field$/ do Given /^I update my new user view to include the file upload field$/ do
if framework_version?("3") steps %{
steps %{ Given I overwrite "app/views/users/new.html.erb" with:
Given I overwrite "app/views/users/new.html.erb" with: """
""" <%= form_for @user, :html => { :multipart => true } do |f| %>
<%= form_for @user, :html => { :multipart => true } do |f| %> <%= f.label :name %>
<%= f.label :name %> <%= f.text_field :name %>
<%= f.text_field :name %> <%= f.label :attachment %>
<%= f.label :attachment %> <%= f.file_field :attachment %>
<%= f.file_field :attachment %> <%= submit_tag "Submit" %>
<%= submit_tag "Submit" %> <% end %>
<% end %> """
""" }
}
else
steps %{
Given I overwrite "app/views/users/new.html.erb" with:
"""
<% form_for @user, :html => { :multipart => true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= submit_tag "Submit" %>
<% end %>
"""
}
end
end end
Given /^I update my user view to include the attachment$/ do Given /^I update my user view to include the attachment$/ do
...@@ -89,7 +74,7 @@ Given /^I reload my application$/ do ...@@ -89,7 +74,7 @@ Given /^I reload my application$/ do
Rails::Application.reload! Rails::Application.reload!
end end
When %r{I turn off class caching} do When /^I turn off class caching$/ do
in_current_dir do in_current_dir do
file = "config/environments/test.rb" file = "config/environments/test.rb"
config = IO.read(file) config = IO.read(file)
...@@ -99,30 +84,6 @@ When %r{I turn off class caching} do ...@@ -99,30 +84,6 @@ When %r{I turn off class caching} do
end end
end end
Given /^I update my application to use Bundler$/ do
if framework_version?("2")
boot_config_template = File.read('features/support/fixtures/boot_config.txt')
preinitializer_template = File.read('features/support/fixtures/preinitializer.txt')
gemfile_template = File.read('features/support/fixtures/gemfile.txt')
in_current_dir do
content = File.read("config/boot.rb").sub(/Rails\.boot!/, boot_config_template)
File.open("config/boot.rb", "w") { |file| file.write(content) }
File.open("config/preinitializer.rb", "w") { |file| file.write(preinitializer_template) }
File.open("Gemfile", "w") { |file| file.write(gemfile_template.sub(/RAILS_VERSION/, framework_version)) }
end
end
end
Given /^I add the paperclip rake task to a Rails 2.3 application$/ do
if framework_version?("2.3")
require 'fileutils'
source = File.expand_path('lib/tasks/paperclip.rake')
destination = in_current_dir { File.expand_path("lib/tasks") }
FileUtils.cp source, destination
append_to "Rakefile", "require 'paperclip'"
end
end
Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path| Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
expected = IO.read(path) expected = IO.read(path)
actual = if web_file.match %r{^https?://} actual = if web_file.match %r{^https?://}
...@@ -155,28 +116,3 @@ end ...@@ -155,28 +116,3 @@ end
When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname| When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
comment_out_gem_in_gemfile gemname comment_out_gem_in_gemfile gemname
end end
module FileHelpers
def append_to(path, contents)
in_current_dir do
File.open(path, "a") do |file|
file.puts
file.puts contents
end
end
end
def append_to_gemfile(contents)
append_to('Gemfile', contents)
end
def comment_out_gem_in_gemfile(gemname)
in_current_dir do
gemfile = File.read("Gemfile")
gemfile.sub!(/^(\s*)(gem\s*['"]#{gemname})/, "\\1# \\2")
File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
end
end
end
World(FileHelpers)
module FileHelpers
def append_to(path, contents)
in_current_dir do
File.open(path, "a") do |file|
file.puts
file.puts contents
end
end
end
def append_to_gemfile(contents)
append_to('Gemfile', contents)
end
def comment_out_gem_in_gemfile(gemname)
in_current_dir do
gemfile = File.read("Gemfile")
gemfile.sub!(/^(\s*)(gem\s*['"]#{gemname})/, "\\1# \\2")
File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
end
end
end
World(FileHelpers)
...@@ -32,15 +32,15 @@ module RailsCommandHelpers ...@@ -32,15 +32,15 @@ module RailsCommandHelpers
end end
def new_application_command def new_application_command
framework_version?("3") ? "rails new" : "rails" "rails new"
end end
def generator_command def generator_command
framework_version?("3") ? "script/rails generate" : "script/generate" "script/rails generate"
end end
def runner_command def runner_command
framework_version?("3") ? "script/rails runner" : "script/runner" "script/rails runner"
end end
end end
World(RailsCommandHelpers) World(RailsCommandHelpers)
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
source "http://rubygems.org" source "http://rubygems.org"
gem "jruby-openssl", :platform=>:jruby gem "jruby-openssl", :platform=>:jruby
gem "rails", "~> 2.3.14" gem "rails", "~> 3.0.12"
gem "paperclip", :path=>"../" gem "paperclip", :path=>"../"
gemspec :path=>"../" gemspec :path=>"../"
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
source "http://rubygems.org" source "http://rubygems.org"
gem "jruby-openssl", :platform=>:jruby gem "jruby-openssl", :platform=>:jruby
gem "rails", "~> 3.0.10" gem "rails", "~> 3.1.4"
gem "paperclip", :path=>"../" gem "paperclip", :path=>"../"
gemspec :path=>"../" gemspec :path=>"../"
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
source "http://rubygems.org" source "http://rubygems.org"
gem "jruby-openssl", :platform=>:jruby gem "jruby-openssl", :platform=>:jruby
gem "rails", "~> 3.1.0" gem "rails", "~> 3.2.2"
gem "paperclip", :path=>"../" gem "paperclip", :path=>"../"
gemspec :path=>"../" gemspec :path=>"../"
\ No newline at end of file
# This file was generated by Appraisal
source "http://rubygems.org"
gem "jruby-openssl", :platform=>:jruby
gem "rails", "~> 3.2.0"
gem "paperclip", :path=>"../"
gemspec :path=>"../"
\ No newline at end of file
require File.join(File.dirname(__FILE__), "lib", "paperclip")
require 'paperclip/railtie'
Paperclip::Railtie.insert
...@@ -39,7 +39,8 @@ require 'paperclip/style' ...@@ -39,7 +39,8 @@ require 'paperclip/style'
require 'paperclip/attachment' require 'paperclip/attachment'
require 'paperclip/attachment_options' require 'paperclip/attachment_options'
require 'paperclip/storage' require 'paperclip/storage'
require 'paperclip/callback_compatibility' require 'paperclip/callbacks'
require 'paperclip/glue'
require 'paperclip/missing_attachment_styles' require 'paperclip/missing_attachment_styles'
require 'paperclip/railtie' require 'paperclip/railtie'
require 'logger' require 'logger'
...@@ -175,16 +176,6 @@ module Paperclip ...@@ -175,16 +176,6 @@ module Paperclip
klass.const_defined?(partial_class_name) ? klass.const_get(partial_class_name, false) : klass.const_missing(partial_class_name) klass.const_defined?(partial_class_name) ? klass.const_get(partial_class_name, false) : klass.const_missing(partial_class_name)
end end
end end
rescue ArgumentError => e
# Sadly, we need to capture ArgumentError here because Rails 2.3.x
# ActiveSupport dependency management will try to the constant inherited
# from Object, and fail miserably with "Object is not missing constant X" error
# https://github.com/rails/rails/blob/v2.3.12/activesupport/lib/active_support/dependencies.rb#L124
if e.message =~ /is not missing constant/
raise NameError, "uninitialized constant #{class_name}"
else
raise e
end
end end
def check_for_url_clash(name,url,klass) def check_for_url_clash(name,url,klass)
...@@ -216,18 +207,6 @@ module Paperclip ...@@ -216,18 +207,6 @@ module Paperclip
class InfiniteInterpolationError < PaperclipError #:nodoc: class InfiniteInterpolationError < PaperclipError #:nodoc:
end end
module Glue
def self.included base #:nodoc:
base.extend ClassMethods
base.class_attribute :attachment_definitions if base.respond_to?(:class_attribute)
if base.respond_to?(:set_callback)
base.send :include, Paperclip::CallbackCompatability::Rails3
else
base.send :include, Paperclip::CallbackCompatability::Rails21
end
end
end
module ClassMethods module ClassMethods
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
# is typically a file stored somewhere on the filesystem and has been uploaded by a user. # is typically a file stored somewhere on the filesystem and has been uploaded by a user.
...@@ -312,17 +291,9 @@ module Paperclip ...@@ -312,17 +291,9 @@ module Paperclip
include InstanceMethods include InstanceMethods
if attachment_definitions.nil? if attachment_definitions.nil?
if respond_to?(:class_attribute) self.attachment_definitions = {}
self.attachment_definitions = {}
else
write_inheritable_attribute(:attachment_definitions, {})
end
else else
if respond_to?(:class_attribute) self.attachment_definitions = self.attachment_definitions.dup
self.attachment_definitions = self.attachment_definitions.dup
else
write_inheritable_attribute(:attachment_definitions, self.attachment_definitions.dup)
end
end end
attachment_definitions[name] = Paperclip::AttachmentOptions.new(options) attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
...@@ -433,11 +404,7 @@ module Paperclip ...@@ -433,11 +404,7 @@ module Paperclip
# Returns the attachment definitions defined by each call to # Returns the attachment definitions defined by each call to
# has_attached_file. # has_attached_file.
def attachment_definitions def attachment_definitions
if respond_to?(:class_attribute) self.attachment_definitions
self.attachment_definitions
else
read_inheritable_attribute(:attachment_definitions)
end
end end
end end
......
module Paperclip module Paperclip
module CallbackCompatability module Callbacks
module Rails21 def self.included(base)
def self.included(base) base.extend(Defining)
base.extend(Defining) base.send(:include, Running)
base.send(:include, Running)
end
module Defining
def define_paperclip_callbacks(*args)
args.each do |callback|
define_callbacks("before_#{callback}")
define_callbacks("after_#{callback}")
end
end
end
module Running
def run_paperclip_callbacks(callback, opts = nil, &blk)
# The overall structure of this isn't ideal since after callbacks run even if
# befores return false. But this is how rails 3's callbacks work, unfortunately.
if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false
blk.call
end
run_callbacks(:"after_#{callback}"){ |result, object| result == false }
end
end
end end
module Rails3 module Defining
def self.included(base) def define_paperclip_callbacks(*callbacks)
base.extend(Defining) define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
base.send(:include, Running) callbacks.each do |callback|
end eval <<-end_callbacks
def before_#{callback}(*args, &blk)
module Defining set_callback(:#{callback}, :before, *args, &blk)
def define_paperclip_callbacks(*callbacks) end
define_callbacks *[callbacks, {:terminator => "result == false"}].flatten def after_#{callback}(*args, &blk)
callbacks.each do |callback| set_callback(:#{callback}, :after, *args, &blk)
eval <<-end_callbacks end
def before_#{callback}(*args, &blk) end_callbacks
set_callback(:#{callback}, :before, *args, &blk)
end
def after_#{callback}(*args, &blk)
set_callback(:#{callback}, :after, *args, &blk)
end
end_callbacks
end
end end
end end
end
module Running module Running
def run_paperclip_callbacks(callback, opts = nil, &block) def run_paperclip_callbacks(callback, opts = nil, &block)
run_callbacks(callback, opts, &block) run_callbacks(callback, opts, &block)
end
end end
end end
end end
end end
require 'paperclip/callbacks'
module Paperclip
module Glue
def self.included base #:nodoc:
base.extend ClassMethods
base.send :include, Callbacks
base.class_attribute :attachment_definitions
end
end
end
...@@ -2,28 +2,25 @@ require 'paperclip' ...@@ -2,28 +2,25 @@ require 'paperclip'
require 'paperclip/schema' require 'paperclip/schema'
module Paperclip module Paperclip
if defined? Rails::Railtie require 'rails'
require 'rails' class Railtie < Rails::Railtie
class Railtie < Rails::Railtie initializer 'paperclip.insert_into_active_record' do
initializer 'paperclip.insert_into_active_record' do ActiveSupport.on_load :active_record do
ActiveSupport.on_load :active_record do Paperclip::Railtie.insert
Paperclip::Railtie.insert
end
end
rake_tasks do
load "tasks/paperclip.rake"
end end
end end
rake_tasks do
load "tasks/paperclip.rake"
end
end end
class Railtie class Railtie
def self.insert def self.insert
Paperclip.options[:logger] = Rails.logger if defined?(Rails) Paperclip.options[:logger] = Rails.logger if defined?(Rails)
if defined?(ActiveRecord) if defined?(ActiveRecord)
ActiveRecord::Base.send(:include, Paperclip::Glue)
Paperclip.options[:logger] = ActiveRecord::Base.logger Paperclip.options[:logger] = ActiveRecord::Base.logger
ActiveRecord::Base.send(:include, Paperclip::Glue)
ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema) ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema) ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema) ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
......
...@@ -66,7 +66,11 @@ def reset_class class_name ...@@ -66,7 +66,11 @@ def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue) ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base)) klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.class_eval{ include Paperclip::Glue }
klass.class_eval do
include Paperclip::Glue
end
klass klass
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment