acts_as_assets 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,28 @@
1
1
  class ActsAsAssets::AssetsController < ApplicationController
2
+ include ActsAsAssets::AssetsHelper
3
+ helper_method :destroy_path
2
4
  before_filter "assign_root_model"
3
5
  before_filter "assign_target", :only => [:index,:destroy]
4
6
 
5
7
  def index
6
8
  load_assets
7
- render 'acts_as_assets/assets/index', :layout => false
9
+ respond_to do |format|
10
+ format.html{render :layout => false}
11
+ format.json{render :json => @assets}
12
+ end
8
13
  end
9
14
 
10
15
  def create
11
16
  name = root_model_name
12
- klazz = ([name.pluralize.camelize] << params[:type]).join('::')
17
+ klazz = ([name.pluralize.camelize] << camelize_type.flatten).join('::')
13
18
  @asset = klazz.constantize.create!(:asset => params[:file],
14
19
  "#{name}".to_sym => instance_variable_get("@#{name}".to_sym))
15
20
 
16
21
  respond_to do |format|
17
22
  if @asset.valid?
18
- format.js { render :json => {:success => true} }
23
+ format.js
19
24
  else
20
- format.js { render :json =>{:success => false, :errors => @asset.errors} }
25
+ format.js { render :json =>{:errors => @asset.errors}, :status => 406 }
21
26
  end
22
27
  end
23
28
  end
@@ -25,7 +30,7 @@ class ActsAsAssets::AssetsController < ApplicationController
25
30
  def destroy
26
31
 
27
32
  begin
28
- @asset = instance_variable_get("@#{root_model_name}").send(:assets).find_by_id(params[:id])
33
+ @asset = instance_variable_get("@#{root_model_name}").send(:assets).find_by_id(params[:asset_id])
29
34
  @asset.destroy
30
35
  rescue Exception => e
31
36
  error = e.message
@@ -33,9 +38,9 @@ class ActsAsAssets::AssetsController < ApplicationController
33
38
 
34
39
  respond_to do |format|
35
40
  if error.nil?
36
- format.js { render 'acts_as_assets/assets/destroy'}
41
+ format.js
37
42
  else
38
- format.js { render :json => {:success => false, :errors => error} }
43
+ format.js { render :json => {:errors => error}, :status => 406 }
39
44
  end
40
45
  end
41
46
 
@@ -46,7 +51,7 @@ class ActsAsAssets::AssetsController < ApplicationController
46
51
 
47
52
  def load_assets
48
53
  name = root_model_name
49
- klazz = ([name.pluralize.camelize] << params[:type]).join('::')
54
+ klazz = ([name.pluralize.camelize] << camelize_type.flatten).join('::')
50
55
  @assets = klazz.constantize.send(:where,"#{name}_id".to_sym => params["#{name}_id".to_sym])
51
56
  end
52
57
 
@@ -63,12 +68,9 @@ class ActsAsAssets::AssetsController < ApplicationController
63
68
  @target = params[:target]
64
69
  end
65
70
 
66
- def destroy_path doc
67
- name = root_model_name
68
- method = "#{name.pluralize}_assets_destroy_path"
69
- send(method.to_sym, instance_variable_get("@#{name}"), :asset_id => doc.id, :target => @target)
71
+ # takes a type params string like "my/asset/type_of_documento" and convert into [My,Asset,TypeOfDocument]
72
+ def camelize_type
73
+ params[:type].split('/').collect{|i|i.camelize}
70
74
  end
71
- helper_method :destroy_path
72
-
73
75
 
74
76
  end
@@ -0,0 +1,9 @@
1
+ module ActsAsAssets::AssetsHelper
2
+
3
+ def destroy_path doc, target
4
+ name = ActiveSupport::Inflector.underscore(self.class.to_s.split('::').first.singularize)
5
+ method = "#{name.pluralize}_assets_destroy_path"
6
+ send(method.to_sym, instance_variable_get("@#{name}"), :asset_id => doc.id, :target => target)
7
+ end
8
+
9
+ end
@@ -0,0 +1 @@
1
+ {"success":"true"}
@@ -1 +1 @@
1
- K.ajax.loadDiv("<%= @target %>", true);
1
+ {"success":"true"}
@@ -3,7 +3,7 @@
3
3
 
4
4
  <div class="grid_6 document_link">
5
5
  <%= link_to doc.asset.to_file.original_filename, doc.asset.url %>
6
- <%= link_to t('destroy'), destroy_path(doc),
6
+ <%= link_to t('destroy'), destroy_path(doc,@target),
7
7
  :method => :delete, :confirm => 'Sei Sicuro?', :remote => true %>
8
8
  </div>
9
9
 
@@ -0,0 +1,2 @@
1
+ acts_as_assets:
2
+ unique_asset_error: 'Un solo documento di questo tipo può essere caricato. Eliminare il file attuale per sostituirlo.'
@@ -1,3 +1,6 @@
1
+ require "acts_as_assets/paperclip/interpolations"
2
+ require "acts_as_assets/unique_asset"
3
+
1
4
  module ActsAsAssets
2
5
 
3
6
  module Base
@@ -31,6 +34,10 @@ module ActsAsAssets
31
34
 
32
35
  module InstanceMethods
33
36
 
37
+ def acting_as_assets?
38
+ true
39
+ end
40
+
34
41
  private
35
42
 
36
43
  def touch_counter
@@ -0,0 +1,13 @@
1
+ RSpec::Matchers.define :act_as_assets do
2
+ match do |model|
3
+ model.new if model.respond_to?(:new)
4
+ model.respond_to?(:acting_as_assets?) && model.acting_as_assets?
5
+ end
6
+ end
7
+
8
+ RSpec::Matchers.define :act_as_unique_asset do
9
+ match do |model|
10
+ model.new if model.respond_to?(:new)
11
+ model.respond_to?(:acts_as_assets_check_uniqness)
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module ActsAsAssets::UniqueAsset
2
+
3
+ def self.included(klass)
4
+ klass.send(:include, InstanceMethods)
5
+ klass.send(:validate,:acts_as_assets_check_uniqness, :on => :create)
6
+ end
7
+
8
+ module InstanceMethods
9
+
10
+ def acts_as_assets_check_uniqness
11
+ key = "#{self.class.root_model}_id"
12
+ obj = self.class.send("find_by_#{key}".to_sym,self.send(key.to_sym))
13
+ if obj
14
+ errors.add(self.type, I18n.translate('acts_as_assets.unique_asset_error'))
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsAssets
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  require "acts_as_assets/engine"
2
- require "acts_as_assets/interpolations"
3
2
  require "acts_as_assets/base"
3
+ require "acts_as_assets/matchers/rspec" if defined?(RSpec)
@@ -4,14 +4,6 @@ describe "ActsAsAssets" do
4
4
 
5
5
  let(:book) { Book.create!(:title => "my new book") }
6
6
 
7
- before :all do
8
- @docs = []
9
- end
10
-
11
- after :all do
12
- @docs.each { |d| d.destroy }
13
- end
14
-
15
7
  describe "model that acts as assets" do
16
8
  subject { Books::Asset.new }
17
9
  it { should belong_to(:book) }
@@ -88,9 +80,9 @@ describe "ActsAsAssets" do
88
80
 
89
81
  it "should increase counter by one if a document of the same type are created" do
90
82
  dc = Books::Assets::TestDoc.create!
91
- @docs << dc
83
+ @dtbd << dc
92
84
  dc2 = Books::Assets::TestDoc.create!
93
- @docs << dc2
85
+ @dtbd << dc2
94
86
  Books::Asset.find(dc.id).counter.should eq 1
95
87
  Books::Asset.find(dc2.id).counter.should eq 2
96
88
  end
@@ -106,14 +98,14 @@ describe "ActsAsAssets" do
106
98
  it "should interpolate the correct path for a subclass instance" do
107
99
  b = book
108
100
  doc = Books::Assets::TestDoc.create! :asset => jpg_test, :book => b
109
- @docs << doc
101
+ @dtbd << doc
110
102
  doc.asset.path.should eq "public/system/books/#{b.id}/assets/test_doc.jpg"
111
103
  end
112
104
 
113
105
  it "should interpolate the correct url for a subclass instance" do
114
106
  b = book
115
107
  doc = Books::Assets::TestDoc.create! :asset => jpg_test, :book => b
116
- @docs << doc
108
+ @dtbd << doc
117
109
  doc.asset.url.should match /\/books\/#{b.id}\/assets\/#{doc.id}/
118
110
  end
119
111
 
@@ -5,15 +5,6 @@ describe Books::AssetsController do
5
5
  let(:book) { Book.create!(:title => "my new book") }
6
6
  let(:target){'target_div'}
7
7
 
8
- before :all do
9
- @docs = []
10
- end
11
-
12
- after :all do
13
- @docs.each { |d| d.destroy }
14
- end
15
-
16
-
17
8
  context "given a Books::AssetsController controller " do
18
9
 
19
10
  describe "root_model_name" do
@@ -26,11 +17,11 @@ describe Books::AssetsController do
26
17
  before do
27
18
  @book = book
28
19
  @asset = Books::Assets::TestDoc.create!(:book => @book, :asset => jpg_test)
29
- @docs << @asset
30
- get :index, :book_id => @book.id, :type => ["Assets","TestDoc"]
20
+ @dtbd << @asset
21
+ get :index, :book_id => @book.id, :type => "Assets/TestDoc"
31
22
  end
32
23
  it "should return /books/id/assets/asset_id" do
33
- subject.send(:destroy_path,@asset).should eq "/books/#{@book.id}/assets/#{@asset.id}"
24
+ subject.send(:destroy_path,@asset, target).should eq "/books/#{@book.id}/assets/#{@asset.id}?target=#{target}"
34
25
  end
35
26
  end
36
27
 
@@ -42,7 +33,7 @@ describe Books::AssetsController do
42
33
 
43
34
  before do
44
35
  @book = book
45
- get :index, :book_id => @book.id, :type => ["Assets","TestDoc"]
36
+ get :index, :book_id => @book.id, :type => "Assets/TestDoc"
46
37
  end
47
38
 
48
39
  it "should assign @book" do
@@ -55,25 +46,46 @@ describe Books::AssetsController do
55
46
 
56
47
  describe "index" do
57
48
 
58
- before :each do
59
- @book = book
60
- @asset = Books::Assets::TestDoc.create!(:book => @book, :asset => jpg_test)
61
- @docs << @asset
62
- get :index, :book_id => book.id, :type => ["Assets","TestDoc"], :target => target, :format => :js
63
- end
49
+ context "format html" do
50
+
51
+ before :each do
52
+ @book = book
53
+ @asset = Books::Assets::TestDoc.create!(:book => @book, :asset => jpg_test)
54
+ @dtbd << @asset
55
+ get :index, :book_id => book.id, :type => "Assets/TestDoc", :target => target, :format => :html
56
+ end
57
+
58
+ it "should assign variables" do
59
+ should assign_to(:assets)
60
+ should assign_to(:target)
61
+ should assign_to(:book)
62
+ end
63
+
64
+ it{should respond_with(:success)}
65
+
66
+ it{should render_template('acts_as_assets/assets/index')}
67
+
68
+ it "should return the correct formatted view" do
69
+ response.body.should match(/#{@asset.asset.to_file.original_filename}/)
70
+ end
64
71
 
65
- it "should assign variables" do
66
- should assign_to(:assets)
67
- should assign_to(:target)
68
- should assign_to(:book)
69
72
  end
70
73
 
71
- it{should respond_with(:success)}
74
+ context "format json" do
72
75
 
73
- it{should render_template('acts_as_assets/assets/index')}
76
+ before :each do
77
+ @book = book
78
+ @asset = Books::Assets::TestDoc.create!(:book => @book, :asset => jpg_test)
79
+ @dtbd << @asset
80
+ get :index, :book_id => book.id, :type => "Assets/TestDoc", :target => target, :format => :json
81
+ end
82
+
83
+ it{should_not render_template('acts_as_assets/assets/index')}
84
+
85
+ it "should return assets in json format" do
86
+ ActiveSupport::JSON.decode(response.body).first["asset_file_name"].should eq @asset.asset_file_name
87
+ end
74
88
 
75
- it "should return the correct formatted view" do
76
- response.body.should match(/#{@asset.asset.to_file.original_filename}/)
77
89
  end
78
90
 
79
91
  end
@@ -82,8 +94,8 @@ describe Books::AssetsController do
82
94
 
83
95
  before do
84
96
  @book = book
85
- post :create, :book_id => @book.id, :type => ["Assets","TestDoc"], :file => jpg_test, :format => "js"
86
- @docs << assigns(:asset)
97
+ post :create, :book_id => @book.id, :type => "Assets/TestDoc", :file => jpg_test, :format => "js"
98
+ @dtbd << assigns(:asset)
87
99
  end
88
100
 
89
101
  it "should assign variables" do
@@ -91,7 +103,8 @@ describe Books::AssetsController do
91
103
  should assign_to(:book)
92
104
  end
93
105
 
94
- it "should be success returning the correct content type" do
106
+ it "should be success returning the correct json formatted text and the correct content type" do
107
+ ActiveSupport::JSON.decode(response.body)["success"].should be_true
95
108
  should respond_with_content_type(:js)
96
109
  should respond_with(:success)
97
110
  end
@@ -107,7 +120,8 @@ describe Books::AssetsController do
107
120
  before :each do
108
121
  @book = book
109
122
  @asset = Books::Assets::TestDoc.create!(:book => @book, :asset => jpg_test)
110
- delete :destroy, :book_id => @book.id, :id => @asset.id, :format => "js", :target => target
123
+ @dtbd << @asset
124
+ delete :destroy, :book_id => @book.id, :asset_id => @asset.id, :format => "js", :target => target
111
125
  end
112
126
 
113
127
  it "should assign variables" do
@@ -116,17 +130,14 @@ describe Books::AssetsController do
116
130
  should assign_to(:book)
117
131
  end
118
132
 
119
- it "should be success returning the correct content type" do
133
+ it "should be success returning the correct content type and a json string reporting succes == true" do
134
+ ActiveSupport::JSON.decode(response.body)["success"].should be_true
120
135
  should respond_with_content_type(:js)
121
136
  should respond_with(:success)
122
137
  end
123
138
 
124
139
  it{should render_template('acts_as_assets/assets/destroy')}
125
140
 
126
- it "should return the correct js to execute" do
127
- response.body.should match(/#{target}/)
128
- end
129
-
130
141
  it "should correctly delete the file" do
131
142
  File.exist?(File.expand_path(@asset.asset.path)).should be_false
132
143
  end