can_be 0.2.0 → 0.2.1

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 CHANGED
@@ -5,3 +5,7 @@ Initial release.
5
5
  ## Version 0.2.0
6
6
 
7
7
  Added details functionality for storing custom fields per type.
8
+
9
+ ## Version 0.2.1
10
+
11
+ Changed the way that the details models are added.
data/README.md CHANGED
@@ -75,19 +75,24 @@ end
75
75
 
76
76
  ### Details Model Configuration
77
77
 
78
- In order to wire up a model to be a CanBe details model, you will need
79
- to call the `can_be_detail` method on that model.
78
+ In order to wire up a model to be a CanBe details model, you will need to call the `can_be_detail` method on that model.
80
79
 
81
80
  ```ruby
82
81
  class HomeAddressDetail < ActiveRecord::Base
83
- can_be_detail :address, :home_address
82
+ can_be_detail :address
84
83
  end
85
84
  ```
86
- The `can_be_detail` method take in two parameters.
85
+ The `can_be_detail` method take in one parameter. The parameter is the link to the CanBe model. This must be a symbol that will reference the CanBe model. In order to create the proper symbol, you can execute the following into your Rails console: `<ModelName>.name.underscore.to_sym`. Here is an example: `Address.name.underscore.to_sym`. In the above example, this will be used for the `Address` CanBe model.
87
86
 
88
- The first is the link to the CanBe model. This must be a symbol that will reference the CanBe model. In order to create the proper symbol, you can execute the following into your Rails console: `<ModelName>.name.underscore.to_sym`. Here is an example: `Address.name.underscore.to_sym`. In the above example, this will be used for the `Address` CanBe model.
87
+ You will also need to call the `add_details_model` method in the `can_be` block, passing in the CanBe type and a symbol that represets the details model class.
89
88
 
90
- The second parameter is the CanBe type that this model is to be used for. In the example above, the `HomeAddressDetail` model will used for the `:home_address` CanBe type.
89
+ ```ruby
90
+ class Address < ActiveRecord::Base
91
+ can_be :home_address, :work_address, :vacation_address do
92
+ add_details_model :home_address, :home_address_detail
93
+ end
94
+ end
95
+ ```
91
96
 
92
97
  ## Usage
93
98
 
data/lib/can_be/config.rb CHANGED
@@ -25,9 +25,8 @@ module CanBe
25
25
  @details ||= {}
26
26
  end
27
27
 
28
- def self.add_detail_model(klass, can_be_class, can_be_type)
29
- config = can_be_class.to_s.camelize.constantize.can_be_config
30
- config.details[can_be_type] = klass.name
28
+ def add_details_model(can_be_type, model_symbol)
29
+ self.details[can_be_type] = model_symbol
31
30
  end
32
31
  end
33
32
  end
@@ -9,7 +9,7 @@ module CanBe
9
9
  @can_be_config ||= CanBe::Config.new
10
10
  end
11
11
 
12
- def can_be(*types)
12
+ def can_be(*types, &block)
13
13
  if types.last.is_a?(Hash)
14
14
  options = types.last
15
15
  types.delete types.last
@@ -18,11 +18,12 @@ module CanBe
18
18
  can_be_config.types = types
19
19
  can_be_config.parse_options options if options
20
20
 
21
+ can_be_config.instance_eval(&block)if block_given?
22
+
21
23
  CanBe::Builder::CanBe.build(self)
22
24
  end
23
25
 
24
- def can_be_detail(can_be_model, can_be_type)
25
- CanBe::Config.add_detail_model self, can_be_model, can_be_type
26
+ def can_be_detail(can_be_model)
26
27
  CanBe::Builder::CanBeDetail.build(self, can_be_model)
27
28
  end
28
29
  end
@@ -49,7 +49,7 @@ module CanBe
49
49
  classname = @config.details[t.to_sym]
50
50
 
51
51
  if classname
52
- @model.details = classname.constantize.new
52
+ @model.details = classname.to_s.camelize.constantize.new
53
53
  else
54
54
  @model.details_id = nil
55
55
  @model.details_type = nil
@@ -1,3 +1,3 @@
1
1
  module CanBe
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -48,14 +48,14 @@ describe CanBe::Config do
48
48
 
49
49
  context "#add_detail_model" do
50
50
  it "adds the detail information" do
51
- CanBe::Config.add_detail_model ConfigSpecDetail, :config_spec_model, :type1
52
- ConfigSpecModel.can_be_config.details[:type1].should == ConfigSpecDetail.name
51
+ subject.add_details_model :type1, :config_spec_model
52
+ subject.details[:type1].should == :config_spec_model
53
53
  end
54
54
 
55
55
  it "adds the detail information for a second record" do
56
- CanBe::Config.add_detail_model ConfigSpecDetail, :config_spec_model, :type1
57
- CanBe::Config.add_detail_model ConfigSpecDetail2, :config_spec_model, :type2
58
- ConfigSpecModel.can_be_config.details[:type2].should == ConfigSpecDetail2.name
56
+ subject.add_details_model :type1, :config_spec_model
57
+ subject.add_details_model :type1, :config_spec_model2
58
+ subject.details[:type1].should == :config_spec_model2
59
59
  end
60
60
  end
61
61
  end
@@ -12,15 +12,18 @@ class Person < ActiveRecord::Base
12
12
  end
13
13
 
14
14
  class Upload < ActiveRecord::Base
15
- can_be :image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload
15
+ can_be :image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload do
16
+ add_details_model :image_upload, :image_upload_detail
17
+ add_details_model :video_upload, :video_upload_detail
18
+ end
16
19
  end
17
20
 
18
21
  class ImageUploadDetail < ActiveRecord::Base
19
- can_be_detail :upload, :image_upload
22
+ can_be_detail :upload
20
23
  end
21
24
 
22
25
  class VideoUploadDetail < ActiveRecord::Base
23
- can_be_detail :upload, :video_upload
26
+ can_be_detail :upload
24
27
  end
25
28
 
26
29
  class ThumbnailUploadDetail < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_be
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: