friendly-attributes 0.1.0 → 0.2.0

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.
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ Changelog
2
+ =========
3
+
4
+ ### 0.2.0
5
+
6
+ * (ihoka) Extended the DSL to allow passing a hash of options when defining friendly_attributes.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{friendly-attributes}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Istvan Hoka"]
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ "CHANGELOG.md",
21
22
  "Gemfile",
22
23
  "Gemfile.lock",
23
24
  "LICENSE.txt",
@@ -1,7 +1,22 @@
1
1
  module FriendlyAttributes
2
2
  module ClassMethods
3
- def friendly_details(klass, &block)
4
- DetailsDelegator.new(klass, self, &block)
3
+ def friendly_details(*args, &block)
4
+ klass = args.shift
5
+ options = args.extract_options!
6
+
7
+ delegate_options = proc {
8
+ options.each do |key, value|
9
+ if Array === value
10
+ value.each { |v| delegated_attribute v, key }
11
+ else
12
+ delegated_attribute value, key
13
+ end
14
+ end
15
+ }
16
+
17
+ DetailsDelegator.new(klass, self, &block).tap do |dd|
18
+ dd.instance_eval(&delegate_options)
19
+ end
5
20
  end
6
21
 
7
22
  def friendly_mount_uploader(name, klass)
@@ -24,7 +24,7 @@ module FriendlyAttributes
24
24
  end
25
25
  end
26
26
 
27
- self.instance_eval(&block)
27
+ self.instance_eval(&block) if block_given?
28
28
  end
29
29
 
30
30
  def delegated_attribute(name, klass)
@@ -14,9 +14,51 @@ describe FriendlyAttributes::ClassMethods do
14
14
  class FakeUploader; end
15
15
 
16
16
  describe ".friendly_details" do
17
- it "instantiates a new DetailsDelegator" do
18
- FriendlyAttributes::DetailsDelegator.should_receive(:new).with(friendly_model, ar_model, &initializer).and_return(details_delegator)
19
- ar_model.friendly_details(friendly_model, &initializer).should == details_delegator
17
+ let(:options) do
18
+ {
19
+ String => :foo,
20
+ Integer => [:bar, :baz]
21
+ }
22
+ end
23
+
24
+ before(:each) do
25
+ details_delegator.stub(:delegated_attribute)
26
+ end
27
+
28
+ context "with an initializer block" do
29
+ it "instantiates a new DetailsDelegator" do
30
+ FriendlyAttributes::DetailsDelegator.should_receive(:new).with(friendly_model, ar_model, &initializer).and_return(details_delegator)
31
+ ar_model.friendly_details(friendly_model, &initializer).should == details_delegator
32
+ end
33
+ end
34
+
35
+ context "with an initializer block and options" do
36
+ before(:each) do
37
+ FriendlyAttributes::DetailsDelegator.stub(:new => details_delegator)
38
+ end
39
+
40
+ def do_details
41
+ ar_model.friendly_details(friendly_model, options, &initializer).should == details_delegator
42
+ end
43
+
44
+ it "instantiates a new DetailsDelegator" do
45
+ FriendlyAttributes::DetailsDelegator.should_receive(:new).with(friendly_model, ar_model, &initializer).and_return(details_delegator)
46
+ do_details
47
+ end
48
+
49
+ it "delegates the attributes passed in the options" do
50
+ details_delegator.should_receive(:delegated_attribute).with(:foo, String)
51
+ details_delegator.should_receive(:delegated_attribute).with(:bar, Integer)
52
+ details_delegator.should_receive(:delegated_attribute).with(:baz, Integer)
53
+ do_details
54
+ end
55
+ end
56
+
57
+ context "without a block" do
58
+ it "instantiates a new DetailsDelegator" do
59
+ FriendlyAttributes::DetailsDelegator.should_receive(:new).with(friendly_model, ar_model).and_return(details_delegator)
60
+ ar_model.friendly_details(friendly_model, options).should == details_delegator
61
+ end
20
62
  end
21
63
  end
22
64
 
@@ -11,68 +11,79 @@ describe FriendlyAttributes::DetailsDelegator do
11
11
  let(:friendly_instance) { mock(friendly_model) }
12
12
 
13
13
  describe "initialization" do
14
- context "the Friendly model" do
15
- before(:each) do
16
- details_delegator
17
- end
18
-
19
- it "includes Friendly::Document" do
20
- friendly_model.ancestors.should include(Friendly::Document)
21
- end
14
+ shared_examples_for "DetailsDelegator initialization" do
15
+ context "the Friendly model" do
16
+ before(:each) do
17
+ details_delegator
18
+ end
22
19
 
23
- it "adds the active_record_id attribute" do
24
- friendly_model.attributes.should include(:active_record_id)
25
- end
20
+ it "includes Friendly::Document" do
21
+ friendly_model.ancestors.should include(Friendly::Document)
22
+ end
26
23
 
27
- it "adds an index to active_record_id" do
28
- friendly_model.storage_proxy.index_for_fields([:active_record_id]).should be_an_instance_of(Friendly::Index)
29
- end
30
- end
31
-
32
- context "the ActiveRecord model" do
33
- it "installs the update_friendly_details callback after_save" do
34
- ar_model.should_receive(:after_save).with(:update_friendly_details)
35
- details_delegator
36
- end
37
-
38
- it "installs the destroy_friendly_details callback after_destroy" do
39
- ar_model.should_receive(:after_destroy).with(:destroy_friendly_details)
40
- details_delegator
24
+ it "adds the active_record_id attribute" do
25
+ friendly_model.attributes.should include(:active_record_id)
26
+ end
27
+
28
+ it "adds an index to active_record_id" do
29
+ friendly_model.storage_proxy.index_for_fields([:active_record_id]).should be_an_instance_of(Friendly::Index)
30
+ end
41
31
  end
42
-
43
- context ".details" do
44
- before(:each) do
32
+
33
+ context "the ActiveRecord model" do
34
+ it "installs the update_friendly_details callback after_save" do
35
+ ar_model.should_receive(:after_save).with(:update_friendly_details)
45
36
  details_delegator
46
37
  end
47
-
48
- it "is defined" do
49
- ar_instance.should respond_to(:details)
38
+
39
+ it "installs the destroy_friendly_details callback after_destroy" do
40
+ ar_model.should_receive(:after_destroy).with(:destroy_friendly_details)
41
+ details_delegator
50
42
  end
51
-
52
- it "finds and memoizes the associated Friendly model" do
53
- friendly_model.should_receive(:find_or_build_by_active_record_id).with(ar_instance.id).once.and_return(friendly_instance)
54
- ar_instance.details.should == friendly_instance
55
- ar_instance.details.should == friendly_instance
43
+
44
+ context ".details" do
45
+ before(:each) do
46
+ details_delegator
47
+ end
48
+
49
+ it "is defined" do
50
+ ar_instance.should respond_to(:details)
51
+ end
52
+
53
+ it "finds and memoizes the associated Friendly model" do
54
+ friendly_model.should_receive(:find_or_build_by_active_record_id).with(ar_instance.id).once.and_return(friendly_instance)
55
+ ar_instance.details.should == friendly_instance
56
+ ar_instance.details.should == friendly_instance
57
+ end
56
58
  end
57
59
  end
58
60
  end
61
+
62
+ context "missing initialization block" do
63
+ let(:details_delegator) { FriendlyAttributes::DetailsDelegator.new(friendly_model, ar_model) }
64
+ it_should_behave_like "DetailsDelegator initialization"
65
+ end
59
66
 
60
- context "the initialization block" do
61
- def yielded_inside(instance)
62
- @yielded_instance = instance
63
- end
67
+ context "with initialization block" do
68
+ it_should_behave_like "DetailsDelegator initialization"
64
69
 
65
- let(:initializer) do
66
- example = self
67
-
68
- proc {
69
- example.yielded_inside(self)
70
- }
71
- end
72
-
73
- it "is instance evaled" do
74
- details_delegator
75
- @yielded_instance.should == details_delegator
70
+ context "the initialization block" do
71
+ def yielded_inside(instance)
72
+ @yielded_instance = instance
73
+ end
74
+
75
+ let(:initializer) do
76
+ example = self
77
+
78
+ proc {
79
+ example.yielded_inside(self)
80
+ }
81
+ end
82
+
83
+ it "is instance evaled" do
84
+ details_delegator
85
+ @yielded_instance.should == details_delegator
86
+ end
76
87
  end
77
88
  end
78
89
  end
@@ -5,7 +5,7 @@ describe FriendlyAttributes do
5
5
 
6
6
  describe "creating" do
7
7
  context "with Friendly attributes" do
8
- let(:user) { User.create(:name => "Stan", :email => "stan@example.com") }
8
+ let(:user) { User.create(:name => "Stan", :email => "stan@example.com", :birth_year => 1984, :shoe_size => 42, :subscribed => true) }
9
9
 
10
10
  it "creates an associated Details model with the AR model" do
11
11
  expect do
@@ -14,6 +14,9 @@ describe FriendlyAttributes do
14
14
 
15
15
  user_detail = user.details
16
16
  user_detail.name.should == "Stan"
17
+ user_detail.birth_year.should == 1984
18
+ user_detail.shoe_size.should == 42
19
+ user_detail.subscribed.should be_true
17
20
  end
18
21
  end
19
22
 
data/spec/spec_helper.rb CHANGED
@@ -41,7 +41,7 @@ RSpec.configure do |config|
41
41
  class ::User < ActiveRecord::Base
42
42
  include FriendlyAttributes
43
43
 
44
- friendly_details UserDetails do
44
+ friendly_details(UserDetails, { Integer => [:birth_year, :shoe_size], Friendly::Boolean => :subscribed }) do
45
45
  delegated_attribute :name, String
46
46
  end
47
47
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly-attributes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Istvan Hoka
@@ -345,6 +345,7 @@ extra_rdoc_files:
345
345
  - README.rdoc
346
346
  files:
347
347
  - .document
348
+ - CHANGELOG.md
348
349
  - Gemfile
349
350
  - Gemfile.lock
350
351
  - LICENSE.txt