paperclip_i18n 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,8 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- .rvmrc
5
+ .rvmrc
6
+ nbproject
7
+ spec/paperclip_i18n.db
8
+ spec/uploads
9
+ vendor
data/README.md CHANGED
@@ -1,3 +1,31 @@
1
- # Paperclip I18n
1
+ Paperclip I18n
2
+ ===
3
+
4
+ Is an extension for the [thoughtbot paperclip](https://github.com/thoughtbot/paperclip) plugin to i18n support to file upload.
5
+ Every language has a seperate file scope.
6
+
7
+ * Tested with Rails 3.1
8
+
9
+ # Installation Instruction
10
+
11
+ Add this line to your Gemfile
12
+
13
+ gem 'paranoid_i18n'
14
+
15
+ Bundle update in your console:
16
+
17
+ bundle update
18
+
19
+ You may want to run the generator for final touches (see next step).
20
+
21
+ ## Generator
22
+
23
+ There's a generator which creates the basic model, migration & controller for you:
24
+
25
+ rails g paperclip_i18n
26
+ rake db:migrate
27
+
28
+ You have to specify the controller in your config/routes.rb
29
+
30
+ resources :assets, :only=>[:show]
2
31
 
3
- Is an extension for the [thoughtbot paperclip](https://github.com/thoughtbot/paperclip) plugin to i18n support to file upload. For each language a separate file can be uploaded.
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  require('bundler')
2
+ require('rspec/core/rake_task')
3
+
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ desc('Run RSpec')
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.verbose = false
9
+ end
10
+
11
+ task(:default => :spec)
@@ -11,6 +11,7 @@ class PaperclipI18nGenerator < Rails::Generators::Base
11
11
  end
12
12
  end
13
13
 
14
+ desc "Creates an asset model & migration, prepares an assets controller"
14
15
  def create_paperclip_i18n_migration
15
16
  migration_template('migration.rb', 'db/migrate/paperclip_i18n_tables.rb')
16
17
  copy_file('assets_controller.rb', 'app/controllers/assets_controller.rb')
@@ -1,3 +1,8 @@
1
+ require('active_model')
2
+ require('active_record')
3
+ require('paperclip')
4
+ require('paperclip/railtie')
5
+ Paperclip::Railtie.insert
1
6
  require('paperclip_i18n/has_many_attached_files')
2
7
  require('paperclip_i18n/acts_as_attachment')
3
8
  require('paperclip_i18n/upload_file_validator')
@@ -10,12 +10,13 @@ module PaperclipI18n
10
10
  # This module needs the paperclip plugin to work
11
11
  # http://www.thoughtbot.com/projects/paperclip
12
12
  def acts_as_attachment(options = {})
13
- default_options = { :url => "/uploads/#{Rails.env}/assets/:id_partition/:basename.:style.:extension",
14
- :path => "#{Rails.root}/public/uploads/#{Rails.env}/assets/:id_partition/:basename.:style.:extension" }.merge(options)
13
+ default_options = options
14
+ default_options[:url] = "/uploads/#{Rails.env}/assets/:id_partition/:basename.:style.:extension" if default_options[:url].blank?
15
+ default_options[:path] = "#{Rails.root}/public/uploads/#{Rails.env}/assets/:id_partition/:basename.:style.:extension" if default_options[:path].blank?
15
16
 
16
17
  has_attached_file(:upload, default_options)
17
18
  belongs_to(:attachable, :polymorphic => true)
18
- scope(:i18ns, lambda { where(:upload_language => ::I18n.locale) })
19
+ scope(:i18ns, lambda { where(:upload_language => ::I18n.locale.to_s) })
19
20
  include(::PaperclipI18n::ActsAsAttachment::InstanceMethods)
20
21
  end
21
22
  end
@@ -10,20 +10,20 @@ module PaperclipI18n
10
10
  # This module needs the paperclip plugin to work
11
11
  # http://www.thoughtbot.com/projects/paperclip
12
12
  def has_many_attached_files(options = {})
13
- write_inheritable_attribute(:has_many_attached_files_options, { :counter_cache => options[:counter_cache], :styles => options[:styles] })
14
- class_inheritable_reader(:has_many_attached_files_options)
13
+ class_attribute(:has_many_attached_files_options)
14
+ self.has_many_attached_files_options = { :counter_cache => options[:counter_cache], :styles => options[:styles], :model => options[:model] ||= ::Asset }
15
15
 
16
16
  attr_accessor(:upload)
17
17
  attr_accessor(:current_file_language)
18
18
  after_save(:save_attached_files)
19
- has_many(:assets, :as => :attachable, :dependent => :destroy)
19
+ has_many(:assets, :as => :attachable, :dependent => :destroy, :class_name => self.has_many_attached_files_options[:model].to_s)
20
20
  include(::PaperclipI18n::HasManyAttachedFiles::InstanceMethods)
21
21
  end
22
22
  end
23
23
 
24
24
  module InstanceMethods
25
25
  def save_attached_files
26
- ::Asset.transaction do
26
+ self.has_many_attached_files_options[:model].transaction do
27
27
  if upload.is_a?(Array)
28
28
  upload.each do |data_item|
29
29
  create_or_update_asset(data_item) unless data_item.nil? || data_item.blank?
@@ -36,7 +36,7 @@ module PaperclipI18n
36
36
 
37
37
  def create_or_update_asset(data_item)
38
38
  override_default_styles, normalised_styles = override_default_styles?(data_item.original_filename)
39
- asset = assets.where(:upload_language => current_language).first || Asset.new # is there an attachment, then delete the old
39
+ asset = assets.where(:upload_language => current_language).first || self.has_many_attached_files_options[:model].new # is there an attachment, then delete the old
40
40
  asset.upload.instance_variable_set('@styles', normalised_styles) if override_default_styles
41
41
  asset.upload = data_item
42
42
  asset.upload_language = current_language
@@ -51,10 +51,10 @@ module PaperclipI18n
51
51
  end
52
52
 
53
53
  def override_default_styles?(filename)
54
- if !has_many_attached_files_options[:styles].nil?
54
+ if !self.has_many_attached_files_options[:styles].nil?
55
55
  normalised_styles = {}
56
56
 
57
- has_many_attached_files_options[:styles].each do |name, args|
57
+ self.has_many_attached_files_options[:styles].each do |name, args|
58
58
  dimensions, format = [args, nil].flatten[0..1]
59
59
  format = nil if format.blank?
60
60
 
@@ -74,7 +74,7 @@ module PaperclipI18n
74
74
 
75
75
  # based on the Authlogic plugin (http://agilewebdevelopment.com/plugins/authgasm)
76
76
  def current_language
77
- @current_file_language || ::I18n.locale
77
+ @current_file_language || ::I18n.locale.to_s
78
78
  end
79
79
  end
80
80
  end
@@ -1,4 +1,4 @@
1
- class UploadFileValidator < ActiveModel::EachValidator
1
+ class UploadFileValidator < ::ActiveModel::EachValidator
2
2
  DEFAULT_OPTIONS = { :content_type => nil,
3
3
  :less_then => nil,
4
4
  :presence => true }
@@ -1,3 +1,3 @@
1
1
  module PaperclipI18n
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ["rg@create.at", 'philipp.ullmann@create.at']
11
11
  s.homepage = ""
12
12
  s.summary = 'Adding I18n image upload support to paperclip plugin'
13
- s.description = "This gem depends on the paperclip gem. A separate file can be uploaded for each language."
13
+ s.description = "This gem depends on the paperclip gem and Rails 3.1. A separate file can be uploaded for each language."
14
14
 
15
15
  s.rubyforge_project = 'paperclip_i18n'
16
16
 
@@ -19,5 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ['lib']
21
21
 
22
- s.add_dependency('paperclip', ['>= 2.3.8'])
22
+ s.add_dependency('paperclip', ['>= 2.4.0'])
23
+ s.add_dependency('sqlite3', ['>= 1.3.4'])
24
+ s.add_development_dependency('activemodel', ['>= 3.1.0'])
25
+ s.add_development_dependency('activerecord', ['>= 3.1.0'])
26
+ s.add_development_dependency('rspec', ['>= 2.6.0'])
27
+ s.add_development_dependency('rake', ['0.9.2'])
23
28
  end
@@ -0,0 +1,3 @@
1
+ test:
2
+ :adapter: sqlite3
3
+ :database: spec/paperclip_i18n.db
@@ -0,0 +1,7 @@
1
+ class Attachment < ActiveRecord::Base
2
+ acts_as_attachment :url => '/uploads/:basename.:style.:extension', :path => File.expand_path(File.dirname(__FILE__) + '/uploads')
3
+ end
4
+
5
+ class Document < ActiveRecord::Base
6
+ has_many_attached_files :model => ::Attachment
7
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/models')
3
+
4
+ describe PaperclipI18n do
5
+ describe "has_many_attached_files" do
6
+ before :each do
7
+ I18n.locale = :en
8
+ @file = File.new(File.dirname(__FILE__) + "/upload_dummy.txt")
9
+ @doc = Document.new
10
+ @doc.upload = @file
11
+ @doc.save
12
+ end
13
+ it('should read the file name properly') do
14
+ @doc.assets.i18ns.first.name.should == File.basename('upload_dummy.txt')
15
+ end
16
+ it('should store the file size properly') do
17
+ @doc.assets.i18ns.first.size.should == @file.size
18
+ end
19
+
20
+ context("default language german") do
21
+ before :each do
22
+ @file_german = File.new(File.dirname(__FILE__)+"/upload_dummy.txt")
23
+ I18n.locale = :de
24
+ @doc.upload = @file_german
25
+ @doc.save
26
+ @doc.assets.reload
27
+ end
28
+ it('should have one english upload') do
29
+ I18n.locale = :en
30
+ @doc.assets.i18ns.count == 1
31
+ end
32
+ it('should have one german upload') do
33
+ @doc.assets.i18ns.count == 1
34
+ end
35
+ it('should have 2 uploads in total') do
36
+ @doc.assets.count == 2
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "attachments", :force => true do |t|
3
+ t.string "attachable_type"
4
+ t.integer "attachable_id"
5
+ t.string "upload_file_name"
6
+ t.integer "upload_content_type"
7
+ t.datetime "upload_file_size"
8
+ t.datetime "upload_updated_at"
9
+ t.string "upload_language"
10
+ end
11
+
12
+ create_table "documents", :force => true do |t|
13
+ t.string "title"
14
+ end
15
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'paperclip_i18n'
4
+
5
+ $enabled = false
6
+
7
+ ActiveSupport::Notifications.subscribe(/sql/i) do |*args|
8
+ if $enabled
9
+ event = ActiveSupport::Notifications::Event.new(*args)
10
+ puts "SQL #{event.duration}: #{event.payload[:sql]}"
11
+ end
12
+ end
13
+
14
+ def connect(environment)
15
+ conf = YAML::load(File.open(File.dirname(__FILE__) + '/database.yml'))
16
+ ActiveRecord::Base.establish_connection(conf[environment])
17
+ end
18
+
19
+ # Open ActiveRecord connection
20
+ connect('test')
21
+
22
+ original_stdout = $stdout
23
+ $stdout = StringIO.new
24
+
25
+ begin
26
+ load(File.dirname(__FILE__) + "/schema.rb")
27
+ ensure
28
+ $stdout = original_stdout
29
+ end
30
+
31
+ # Disable logging of paperclip, because it would throw nil.info(..) error message
32
+ Paperclip.options[:log] = false
@@ -0,0 +1 @@
1
+ Hallo Welt!
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rene Gross
@@ -16,25 +16,105 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-04-08 00:00:00 Z
19
+ date: 2011-09-05 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: paperclip
23
22
  prerelease: false
23
+ type: :runtime
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 19
29
+ hash: 31
30
30
  segments:
31
31
  - 2
32
- - 3
33
- - 8
34
- version: 2.3.8
35
- type: :runtime
32
+ - 4
33
+ - 0
34
+ version: 2.4.0
35
+ name: paperclip
36
36
  version_requirements: *id001
37
- description: This gem depends on the paperclip gem. A separate file can be uploaded for each language.
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ type: :runtime
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 1
48
+ - 3
49
+ - 4
50
+ version: 1.3.4
51
+ name: sqlite3
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ type: :development
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 3
64
+ - 1
65
+ - 0
66
+ version: 3.1.0
67
+ name: activemodel
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ prerelease: false
71
+ type: :development
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 3
80
+ - 1
81
+ - 0
82
+ version: 3.1.0
83
+ name: activerecord
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ prerelease: false
87
+ type: :development
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 23
94
+ segments:
95
+ - 2
96
+ - 6
97
+ - 0
98
+ version: 2.6.0
99
+ name: rspec
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ prerelease: false
103
+ type: :development
104
+ requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - "="
108
+ - !ruby/object:Gem::Version
109
+ hash: 63
110
+ segments:
111
+ - 0
112
+ - 9
113
+ - 2
114
+ version: 0.9.2
115
+ name: rake
116
+ version_requirements: *id006
117
+ description: This gem depends on the paperclip gem and Rails 3.1. A separate file can be uploaded for each language.
38
118
  email:
39
119
  - rg@create.at
40
120
  - philipp.ullmann@create.at
@@ -49,17 +129,24 @@ files:
49
129
  - Gemfile
50
130
  - README.md
51
131
  - Rakefile
132
+ - lib/generators/paperclip_i18n/USAGE
133
+ - lib/generators/paperclip_i18n/paperclip_i18n_generator.rb
134
+ - lib/generators/paperclip_i18n/templates/asset.rb
135
+ - lib/generators/paperclip_i18n/templates/assets_controller.rb
136
+ - lib/generators/paperclip_i18n/templates/migration.rb
52
137
  - lib/paperclip_i18n.rb
53
138
  - lib/paperclip_i18n/acts_as_attachment.rb
54
- - lib/paperclip_i18n/generators/paperclip_i18n/USAGE
55
- - lib/paperclip_i18n/generators/paperclip_i18n/paperclip_i18n_generator.rb
56
- - lib/paperclip_i18n/generators/paperclip_i18n/templates/asset.rb
57
- - lib/paperclip_i18n/generators/paperclip_i18n/templates/assets_controller.rb
58
- - lib/paperclip_i18n/generators/paperclip_i18n/templates/migration.rb
59
139
  - lib/paperclip_i18n/has_many_attached_files.rb
60
140
  - lib/paperclip_i18n/upload_file_validator.rb
61
141
  - lib/paperclip_i18n/version.rb
62
142
  - paperclip_i18n.gemspec
143
+ - spec/database.yml
144
+ - spec/models.rb
145
+ - spec/paperclip_i18n_spec.rb
146
+ - spec/schema.rb
147
+ - spec/spec.opts
148
+ - spec/spec_helper.rb
149
+ - spec/upload_dummy.txt
63
150
  homepage: ""
64
151
  licenses: []
65
152
 
@@ -89,9 +176,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
176
  requirements: []
90
177
 
91
178
  rubyforge_project: paperclip_i18n
92
- rubygems_version: 1.7.2
179
+ rubygems_version: 1.8.8
93
180
  signing_key:
94
181
  specification_version: 3
95
182
  summary: Adding I18n image upload support to paperclip plugin
96
- test_files: []
97
-
183
+ test_files:
184
+ - spec/database.yml
185
+ - spec/models.rb
186
+ - spec/paperclip_i18n_spec.rb
187
+ - spec/schema.rb
188
+ - spec/spec.opts
189
+ - spec/spec_helper.rb
190
+ - spec/upload_dummy.txt