draper 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ # Draper Changelog
2
+
3
+ ## 0.16.0
4
+
5
+ * [Automatically prime `view_context`](https://github.com/jcasimir/draper/commit/057ab4e8)
6
+ * [Fixed bug where rspec eq matchers didn't work]((https://github.com/jcasimir/draper/commit/57617b)
7
+ * [Sequel ORM support](https://github.com/jcasimir/draper/commit/7d4942)
8
+ * Fixed issues with newer minitest
9
+ * [Changed the way the `view_context` gets set](https://github.com/jcasimir/draper/commit/0b03d9c)
10
+
11
+ ## 0.15.0
12
+
13
+ * Proper minitest integration
14
+ * [We can properly decorate scoped associations](https://github.com/jcasimir/draper/issues/223)
15
+ * [Fixed awkward eager loading](https://github.com/jcasimir/draper/commit/7dc3510b)
16
+
17
+ ## 0.14.0
18
+
19
+ * [Properly prime the view context in Rails Console](https://github.com/jcasimir/draper/commit/738074f)
20
+ * Make more gems development requirements only
21
+
22
+ ## 0.13.0
23
+
24
+ * Upgraded all dependencies
25
+ * Dropped support for Rubies < 1.9.3
26
+ * `#to_model` has been renamed to `#wrapped_object`
27
+ * Allow proper overriding of special `ActiveModel` methods
28
+
29
+ ## 0.12.3
30
+
31
+ * [Fix i18n issue](https://github.com/jcasimir/draper/issues/202)
32
+
33
+ ## 0.12.2
34
+
35
+ * Fix bug with initializing ammeter
36
+ * Some gems are now development only in the gemspec
37
+ * Fix bug where generated models were still inheriting from `ApplicationDecorator`
38
+
39
+ ## 0.12.0
40
+
41
+ * Added Changelog
42
+ * [Prevented double decoration](https://github.com/jcasimir/draper/issues/173)
43
+ * [`ActiveModel::Errors` support](https://github.com/jcasimir/draper/commit/19496f0c)
44
+ * [Fixed autoloading issue](https://github.com/jcasimir/draper/issues/188)
45
+ * [Re-did generators](https://github.com/jcasimir/draper/commit/9155e58f)
46
+ * [Added capybara integration](https://github.com/jcasimir/draper/commit/57c8678e)
47
+ * Fixed a few bugs with the `DecoratedEnumerableProxy`
48
+
49
+ ## 0.11.1
50
+
51
+ * [Fixed regression, we don't want to introduce a hard dependency on Rails](https://github.com/jcasimir/draper/issues/107)
data/lib/draper/base.rb CHANGED
@@ -64,6 +64,7 @@ module Draper
64
64
  orig_association = model.send(association_symbol)
65
65
 
66
66
  return orig_association if orig_association.nil?
67
+ return decorated_associations[association_symbol] if decorated_associations[association_symbol]
67
68
 
68
69
  orig_association = orig_association.send(options[:scope]) if options[:scope]
69
70
 
@@ -79,7 +80,7 @@ module Draper
79
80
  orig_association.class
80
81
  end
81
82
 
82
- "#{klass}Decorator".constantize.decorate(orig_association, options)
83
+ decorated_associations[association_symbol] = "#{klass}Decorator".constantize.decorate(orig_association, options)
83
84
  end
84
85
  end
85
86
 
@@ -105,6 +106,16 @@ module Draper
105
106
  self.denied += input_denied
106
107
  end
107
108
 
109
+ # Specifies that all methods may *not* be proxied to
110
+ # to the wrapped object.
111
+ #
112
+ # Do not use `.allows` and `.denies` in combination with '.denies_all'
113
+ def self.denies_all
114
+ raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless ((self.allowed == DEFAULT_ALLOWED && self.denied == DEFAULT_DENIED) || (self.allowed != DEFAULT_ALLOWED && self.denied != DEFAULT_DENIED))
115
+ self.denied += [nil] # Add dummy value to denied to prevent calls to #allows. Hacky???
116
+ self.allowed += [nil] # Add dummy value to allowed to prevent calls to #denies
117
+ end
118
+
108
119
  # Specifies a white list of methods which *may* be proxied to
109
120
  # to the wrapped object. When `allows` is used, only the listed
110
121
  # methods and methods defined in the decorator itself will be
@@ -293,5 +304,9 @@ module Draper
293
304
  model.class.reflect_on_association(association)
294
305
  end
295
306
  end
307
+
308
+ def decorated_associations
309
+ @decorated_associations ||= {}
310
+ end
296
311
  end
297
312
  end
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext/object/blank'
1
2
  module Draper
2
3
  class DecoratedEnumerableProxy
3
4
  include Enumerable
@@ -13,6 +14,16 @@ module Draper
13
14
  end
14
15
  alias_method :to_ary, :decorated_collection
15
16
 
17
+ def find(ifnone_or_id = nil, &blk)
18
+ if block_given?
19
+ source.find(ifnone_or_id, &blk)
20
+ else
21
+ obj = decorated_collection.first
22
+ return nil if obj.blank?
23
+ obj.class.find(ifnone_or_id)
24
+ end
25
+ end
26
+
16
27
  def method_missing (method, *args, &block)
17
28
  @wrapped_collection.send(method, *args, &block)
18
29
  end
@@ -1,3 +1,3 @@
1
1
  module Draper
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  module Draper
2
2
  module ViewContext
3
3
  def self.current
4
- Thread.current[:current_view_context] || view_context
4
+ Thread.current[:current_view_context] || ApplicationController.new.view_context
5
5
  end
6
6
 
7
7
  def self.current=(input)
@@ -178,6 +178,23 @@ describe Draper::Base do
178
178
  subject.previous_version.should be_nil
179
179
  end
180
180
  end
181
+
182
+ context "#find" do
183
+ before(:each){ subject.class_eval{ decorates_association :similar_products } }
184
+ context "with a block" do
185
+ it "delegates to #each" do
186
+ subject.similar_products.source.should_receive :find
187
+ subject.similar_products.find {|p| p.title == "title" }
188
+ end
189
+ end
190
+
191
+ context "without a block" do
192
+ it "calls a finder method" do
193
+ subject.similar_products.source.should_not_receive :find
194
+ subject.similar_products.find 1
195
+ end
196
+ end
197
+ end
181
198
  end
182
199
 
183
200
  context('.decorates_associations') do
@@ -693,6 +710,69 @@ describe Draper::Base do
693
710
  end
694
711
  end
695
712
 
713
+ describe "a sample usage with denies_all" do
714
+ let(:subject_with_denies_all){ DecoratorWithDeniesAll.new(source) }
715
+
716
+ [:goodnight_moon, :hello_world, :title].each do |method|
717
+ it "does echo #{method} method" do
718
+ subject_with_denies_all.should_not respond_to(method)
719
+ end
720
+ end
721
+
722
+ let(:using_denies_all_then_denies_all) {
723
+ class DecoratorWithDeniesAllAndDeniesAll < Draper::Base
724
+ denies_all
725
+ denies_all
726
+ end
727
+ }
728
+
729
+ it "allows multple calls to .denies_all" do
730
+ expect { using_denies_all_then_denies_all }.to_not raise_error(ArgumentError)
731
+ end
732
+ end
733
+
734
+ describe "invalid usages of denies_all" do
735
+ let(:using_allows_then_denies_all) {
736
+ class DecoratorWithAllowsAndDeniesAll < Draper::Base
737
+ allows :hello_world
738
+ denies_all
739
+ end
740
+ }
741
+ let(:using_denies_then_denies_all) {
742
+ class DecoratorWithDeniesAndDeniesAll < Draper::Base
743
+ denies :goodnight_moon
744
+ denies_all
745
+ end
746
+ }
747
+ let(:using_denies_all_then_allows) {
748
+ class DecoratorWithDeniesAllAndAllows < Draper::Base
749
+ denies_all
750
+ allows :hello_world
751
+ end
752
+ }
753
+ let(:using_denies_all_then_denies) {
754
+ class DecoratorWithDeniesAllAndDenies < Draper::Base
755
+ denies_all
756
+ denies :goodnight_moon
757
+ end
758
+ }
759
+ it "raises an exception when calling allows then denies_all" do
760
+ expect {using_allows_then_denies_all}.to raise_error(ArgumentError)
761
+ end
762
+
763
+ it "raises an exception when calling denies then denies_all" do
764
+ expect {using_denies_then_denies_all}.to raise_error(ArgumentError)
765
+ end
766
+
767
+ it "raises an exception when calling denies_all then allows" do
768
+ expect {using_denies_all_then_allows}.to raise_error(ArgumentError)
769
+ end
770
+
771
+ it "raises an exception when calling denies_all then denies" do
772
+ expect {using_denies_all_then_denies}.to raise_error(ArgumentError)
773
+ end
774
+ end
775
+
696
776
  context "in a Rails application" do
697
777
  let(:decorator){ DecoratorWithApplicationHelper.decorate(Object.new) }
698
778
 
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
  require 'draper/test/view_context'
3
3
 
4
4
  describe Draper::ViewContext do
5
+ before(:each) do
6
+ Thread.current[:current_view_context] = nil
7
+ end
8
+
5
9
  let(:app_controller) { ApplicationController }
6
10
  let(:app_controller_instance) { app_controller.new }
7
11
 
@@ -13,4 +17,14 @@ describe Draper::ViewContext do
13
17
  ctx = app_controller_instance.view_context
14
18
  Draper::ViewContext.current.should == ctx
15
19
  end
20
+
21
+ describe "view_context priming" do
22
+ let(:app_controller_instance) { double(ApplicationController, :view_context => view_context) }
23
+ let(:view_context) { double("ApplicationController#view_context") }
24
+
25
+ it "primes the view_context when nil" do
26
+ app_controller.should_receive(:new).and_return(app_controller_instance)
27
+ Draper::ViewContext.current.should == view_context
28
+ end
29
+ end
16
30
  end
data/spec/spec_helper.rb CHANGED
@@ -15,6 +15,7 @@ require './spec/support/samples/decorator_with_allows'
15
15
  require './spec/support/samples/decorator_with_multiple_allows'
16
16
  require './spec/support/samples/decorator_with_application_helper'
17
17
  require './spec/support/samples/decorator_with_denies'
18
+ require './spec/support/samples/decorator_with_denies_all'
18
19
  require './spec/support/samples/decorator_with_special_methods'
19
20
  require './spec/support/samples/namespaced_product'
20
21
  require './spec/support/samples/namespaced_product_decorator'
@@ -0,0 +1,3 @@
1
+ class DecoratorWithDeniesAll < Draper::Base
2
+ denies_all
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-11 00:00:00.000000000 Z
13
+ date: 2012-08-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -120,7 +120,7 @@ files:
120
120
  - .rspec
121
121
  - .travis.yml
122
122
  - .yardopts
123
- - CHANGELOG.txt
123
+ - CHANGELOG.markdown
124
124
  - Gemfile
125
125
  - Guardfile
126
126
  - Rakefile
@@ -187,6 +187,7 @@ files:
187
187
  - spec/support/samples/decorator_with_allows.rb
188
188
  - spec/support/samples/decorator_with_application_helper.rb
189
189
  - spec/support/samples/decorator_with_denies.rb
190
+ - spec/support/samples/decorator_with_denies_all.rb
190
191
  - spec/support/samples/decorator_with_multiple_allows.rb
191
192
  - spec/support/samples/decorator_with_special_methods.rb
192
193
  - spec/support/samples/namespaced_product.rb
@@ -214,7 +215,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
215
  version: '0'
215
216
  segments:
216
217
  - 0
217
- hash: -943728829209315877
218
+ hash: 26927068821793701
218
219
  required_rubygems_version: !ruby/object:Gem::Requirement
219
220
  none: false
220
221
  requirements:
@@ -223,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
224
  version: '0'
224
225
  segments:
225
226
  - 0
226
- hash: -943728829209315877
227
+ hash: 26927068821793701
227
228
  requirements: []
228
229
  rubyforge_project: draper
229
230
  rubygems_version: 1.8.24
@@ -245,6 +246,7 @@ test_files:
245
246
  - spec/support/samples/decorator_with_allows.rb
246
247
  - spec/support/samples/decorator_with_application_helper.rb
247
248
  - spec/support/samples/decorator_with_denies.rb
249
+ - spec/support/samples/decorator_with_denies_all.rb
248
250
  - spec/support/samples/decorator_with_multiple_allows.rb
249
251
  - spec/support/samples/decorator_with_special_methods.rb
250
252
  - spec/support/samples/namespaced_product.rb
data/CHANGELOG.txt DELETED
@@ -1,70 +0,0 @@
1
- = Draper Changelog
2
-
3
- = 0.16.0
4
-
5
- * Automatically prime view_context, see 057ab4
6
-
7
- * Fixed bug where rspec eq matchers didn't work, see 57617b
8
-
9
- * Sequel ORM support, see 7d4942
10
-
11
- * Fixed issues with newer minitest
12
-
13
- * changed the way the view_context gets set. See 0b039c
14
-
15
- = 0.15.0
16
-
17
- * Proper minitest integration
18
-
19
- * We can properly decorate scoped associations, see #223
20
-
21
- * Fixed awkward eager loading, see 7dc3510b
22
-
23
- = 0.14.0
24
-
25
- * Properly prime the view context in Rails Console 738074f
26
-
27
- * Make more gems development requirements only
28
-
29
- = 0.13.0
30
-
31
- * Upgraded all dependencies
32
-
33
- * Dropped support for Rubies < 1.9.3
34
-
35
- * #to_model has been renamed to #wrapped_object
36
-
37
- * Allow proper overriding of special ActiveModel methods
38
-
39
- == v0.12.3
40
-
41
- * Fix i18n issue, see #202
42
-
43
- == 0.12.2
44
-
45
- * Fix bug with initializing ammeter
46
-
47
- * Some gems are now development only in the gemspec
48
-
49
- * Fix bug where generated models were still inheriting from
50
- ApplicationDecorator
51
-
52
- == 0.12.0
53
-
54
- * Added Changelog
55
-
56
- * Prevented double decoration, see #173
57
-
58
- * ActiveModel::Errors support, 19496f0c
59
-
60
- * Fixed autoloading issue, see #188
61
-
62
- * Re-did generators, see 9155e58f
63
-
64
- * Added capybara integration, see 57c8678e
65
-
66
- * Fixed a few bugs with the DecoratedEnumerableProxy
67
-
68
- == 0.11.1
69
-
70
- * Fixed regression, we don't want to introduce a hard dependency on Rails. #107