acts_as_inheritable 0.0.2 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61f0d977c0caf99cd17883ba4e0f0e1a945be197
4
- data.tar.gz: 28d9e8a7c3bb486dcfba2970eedbb25d4d4ec22e
3
+ metadata.gz: de7f7a195cd330508b532fbcbac354f6443e1aa9
4
+ data.tar.gz: 1407afe5e36bdabca17bc62bf11513b284a3bc0a
5
5
  SHA512:
6
- metadata.gz: 72b8c954ef211b5f35164393614ab2c062d418df267bfb5a5eeb771d5f4550e1d03f00898ddaacce18e1039fa04ce95e9902200faf3c39b7338f8bfae44b4773
7
- data.tar.gz: e62c7d4bf845b2b476120537a674b6b05c9a8fa179f1d6de929a1c20beaaf082b812d18f6280cb53777b9d5a818a39fa8a5af0149fd886cda674b00505930f24
6
+ metadata.gz: 078ad8bfebed70c26e90d6a166c7d03367f7450c9c2ff01f155e28e965ac05ff9de19ecae784bb362686e96b4461b18467b9497c3832242220c57cb80c440694
7
+ data.tar.gz: 7bf5b0ef3d38faafb2ae3a3a16fd4c56dcac0a1eb52d38b7fb769bfe592b61b74c1255fac9ea0eb5d20e4d9ea15e712849baa71f6ef59924cc3a2e7d63779b82
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # ActsAsInheritable
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/acts_as_inheritable.svg)](https://badge.fury.io/rb/acts_as_inheritable) [![Build Status](https://travis-ci.org/esbanarango/acts_as_inheritable.svg)](https://travis-ci.org/esbanarango/acts_as_inheritable) [![Test Coverage](https://codeclimate.com/github/esbanarango/acts_as_inheritable/badges/coverage.svg)](https://codeclimate.com/github/esbanarango/acts_as_inheritable/coverage) [![Code Climate](https://codeclimate.com/github/esbanarango/acts_as_inheritable/badges/gpa.svg)](https://codeclimate.com/github/esbanarango/acts_as_inheritable)
3
+ [![Gem Version](https://badge.fury.io/rb/acts_as_inheritable.svg)](https://badge.fury.io/rb/acts_as_inheritable) [![Build Status](https://travis-ci.org/esbanarango/acts_as_inheritable.svg)](https://travis-ci.org/esbanarango/acts_as_inheritable) [![Test Coverage](https://codeclimate.com/github/esbanarango/acts_as_inheritable/badges/coverage.svg)](https://codeclimate.com/github/esbanarango/acts_as_inheritable/coverage) [![Code Climate](https://codeclimate.com/github/esbanarango/acts_as_inheritable/badges/gpa.svg)](https://codeclimate.com/github/esbanarango/acts_as_inheritable) [![security](https://hakiri.io/github/esbanarango/acts_as_inheritable/master.svg)](https://hakiri.io/github/esbanarango/acts_as_inheritable/master)
4
4
 
5
- _Acts As Inheritable_ is a Ruby Gem specifically written for Rails/ActiveRecord models. It is mean to be used with the [_Self-Referential Association_](#self-referential-association), or with a model having a `parent` that share the inheritable attributes. This will let you inherit any __attribute__ or __relation__ from the _parent_ model.
5
+ _Acts As Inheritable_ is a Ruby Gem specifically written for Rails/ActiveRecord models. It is meant to be used with the [_Self-Referential Association_](#self-referential-association), or with a model having a `parent` that share the inheritable attributes. This will let you inherit any __attribute__ or __relation__ from the _parent_ model.
6
6
 
7
7
  ### Self-Referential Association
8
8
 
@@ -33,15 +33,14 @@ Or install it yourself as:
33
33
  $ gem install acts_as_inheritable
34
34
 
35
35
  ## Usage
36
+ You can enable ActsAsInheritable by adding `acts_as_inheritable` into your model. You need to define at least one option, either `attributes` or `associations`. Those options are _arrays_ containing the atrributes names or the associations names.
36
37
 
37
38
  ```ruby
38
39
 
39
40
  class Person < ActiveRecord::Base
40
- acts_as_inheritable
41
41
 
42
- # Constants
43
- INHERITABLE_ATTRIBUTES = %w(favorite_color last_name soccer_team)
44
- INHERITABLE_ASSOCIATIONS = %w(shoes pictures clan)
42
+ acts_as_inheritable attributes: %w(favorite_color last_name soccer_team),
43
+ associations: %w(shoes pictures clan)
45
44
 
46
45
  # Associations
47
46
  belongs_to :parent, class_name: 'Person'
@@ -69,21 +68,22 @@ son.favorite_color # => Green
69
68
  By adding `acts_as_inheritable` to your models you will have access to these methods:
70
69
 
71
70
  #### inherit_attributes
71
+ > Signature `inherit_attributes(force = false, not_force_for=[])`
72
+
72
73
  By default this method will only set values that are [blank?](http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F).
73
74
 
74
- ##### params
75
+ ###### params
75
76
  - `force`: Default to true. Set the attribute even if it's _present_.
76
77
  - `not_force_for`: Default to empty array. When setting `force` to _true_, you can also specify the attributes you don't want to overwrite.
77
78
 
78
79
  #### inherit_relations
80
+ > Signature `inherit_relations(model_parent = send(:parent), current = self)`
79
81
 
80
82
  ```ruby
81
83
 
82
84
  class Person < ActiveRecord::Base
83
- acts_as_inheritable
84
85
 
85
- # Constants
86
- INHERITABLE_ASSOCIATIONS = %w(pet)
86
+ acts_as_inheritable associations: %w(pet)
87
87
 
88
88
  # Associations
89
89
  has_one :pet
@@ -103,8 +103,13 @@ son.pet.inspect # => #<Pet id: 2, person_id: 2, name: "Mango", breed: "Golden Re
103
103
 
104
104
  ## Contributing
105
105
 
106
- 1. Fork it ( https://github.com/[my-github-username]/acts_as_inheritable/fork )
106
+ 1. Fork it ( https://github.com/esbanarango/acts_as_inheritable/fork )
107
107
  2. Create your feature branch (`git checkout -b my-new-feature`)
108
108
  3. Commit your changes (`git commit -am 'Add some feature'`)
109
109
  4. Push to the branch (`git push origin my-new-feature`)
110
110
  5. Create a new Pull Request
111
+
112
+ ## Author
113
+
114
+ This was written by [Esteban Arango Medina](http://esbanarango.com) while working at [Blue Sky Cards](https://www.blueskycards.com/).
115
+ >Thanks to [@danielosorio83](https://github.com/danielosorio83) who also wrote part of the logic for this gem.
@@ -2,7 +2,15 @@ require 'active_record'
2
2
  require "acts_as_inheritable/version"
3
3
 
4
4
  module ActsAsInheritable
5
- def acts_as_inheritable
5
+ def acts_as_inheritable(options)
6
+ raise ArgumentError, "Hash expected, got #{options.class.name}" if !options.is_a?(Hash)
7
+ raise ArgumentError, "Empty options" if options[:attributes].empty? && options[:associations].empty?
8
+
9
+ class_attribute :inheritable_configuration
10
+
11
+ self.inheritable_configuration = {}
12
+ self.inheritable_configuration.merge!(options)
13
+
6
14
  class_eval do
7
15
  def has_parent?
8
16
  parent.present?
@@ -12,8 +20,8 @@ module ActsAsInheritable
12
20
  # relations defined on `INHERITABLE_ASSOCIATIONS`. For each instance on
13
21
  # each relation it re-creates it.
14
22
  def inherit_relations(model_parent = send(:parent), current = self)
15
- if model_parent && model_parent.class.const_defined?("INHERITABLE_ASSOCIATIONS")
16
- model_parent.class::INHERITABLE_ASSOCIATIONS.each do |relation|
23
+ if model_parent && model_parent.class.method_defined?(:inheritable_configuration)
24
+ model_parent.class::inheritable_configuration[:associations].each do |relation|
17
25
  parent_relation = model_parent.send(relation)
18
26
  relation_instances = parent_relation.respond_to?(:each) ? parent_relation : [parent_relation].compact
19
27
  relation_instances.each do |relation_instance|
@@ -54,7 +62,7 @@ module ActsAsInheritable
54
62
  end
55
63
  # Relations has a diffeent name
56
64
  unless new_relation.respond_to?(parent_name)
57
- new_relation.class.reflections.keys.each do |reflection|
65
+ new_relation.class.reflections.each_key do |reflection|
58
66
  parent_name = reflection if new_relation.class.reflections[reflection].class_name == model_parent.class.name
59
67
  end
60
68
  end
@@ -64,7 +72,7 @@ module ActsAsInheritable
64
72
  def inherit_attributes(force = false, not_force_for=[])
65
73
  if has_parent?
66
74
  # Attributes
67
- self.class::INHERITABLE_ATTRIBUTES.each do |attribute|
75
+ self.class.inheritable_configuration[:attributes].each do |attribute|
68
76
  current_val = send(attribute)
69
77
  if (force && !not_force_for.include?(attribute)) || current_val.blank?
70
78
  send("#{attribute}=", parent.send(attribute))
@@ -1,3 +1,3 @@
1
1
  module ActsAsInheritable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -5,6 +5,27 @@ require 'support/models'
5
5
 
6
6
  RSpec.describe "ActiveRecord::Base model with #acts_as_inheritable" do
7
7
 
8
+ describe 'describe `acts_as_inheritable` setup' do
9
+ context 'when `acts_as_inheritable` is defined without options' do
10
+ it 'raises an `ArgumentError`' do
11
+ expect{
12
+ class Person < ActiveRecord::Base
13
+ acts_as_inheritable
14
+ end
15
+ }.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+ context 'when `acts_as_inheritable` is defined with empty options' do
19
+ it 'raises an `ArgumentError`' do
20
+ expect{
21
+ class Person < ActiveRecord::Base
22
+ acts_as_inheritable{attributes:[],associations:[]}
23
+ end
24
+ }.to raise_error(ArgumentError)
25
+ end
26
+ end
27
+ end
28
+
8
29
  describe '#inherit_attributes' do
9
30
  let(:person){ create(:person, :with_parent, favorite_color: nil, last_name: nil, soccer_team: nil) }
10
31
  let!(:person_parent) { person.parent }
@@ -1,11 +1,9 @@
1
1
  require 'acts_as_inheritable'
2
2
 
3
3
  class Person < ActiveRecord::Base
4
- acts_as_inheritable
5
4
 
6
- # Constants
7
- INHERITABLE_ATTRIBUTES = %w(favorite_color last_name soccer_team)
8
- INHERITABLE_ASSOCIATIONS = %w(shoes pictures clan toys pet)
5
+ acts_as_inheritable attributes: %w(favorite_color last_name soccer_team),
6
+ associations: %w(shoes pictures clan toys pet)
9
7
 
10
8
  # Associations
11
9
  belongs_to :parent, class_name: 'Person'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_inheritable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esteban Arango Medina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -246,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
246
  version: '0'
247
247
  requirements: []
248
248
  rubyforge_project:
249
- rubygems_version: 2.2.2
249
+ rubygems_version: 2.4.3
250
250
  signing_key:
251
251
  specification_version: 4
252
252
  summary: Inheritable behavior for models with parent.
@@ -257,3 +257,4 @@ test_files:
257
257
  - spec/support/factories.rb
258
258
  - spec/support/migrations.rb
259
259
  - spec/support/models.rb
260
+ has_rdoc: