amoeba 2.1.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 057039190fd1b4431eb35d8e6f0911bff75185ad
4
- data.tar.gz: 4fa23a784b6eed67fab53bfacda76e9974a796d4
3
+ metadata.gz: 8ef5bca881e5bca87a026e6ec8c6094df2542013
4
+ data.tar.gz: c452e76e155190c56519de94abf924534ad8b746
5
5
  SHA512:
6
- metadata.gz: 77579cc4fa532edeaa0637a10ea5f34b8639bea7c5262a6165efc441a4ea7a5d2d2e8cc5c24fcb4af99e801d93c2e454ec08ebed7db8f1dffc28fb3206c11eb3
7
- data.tar.gz: ce4a34f97cc598c79ce74add51e1acacd0e8f8eb024ee7fc7c4352c88f1cce9a9f129c893078ba3f0391fbe3503916cedb40202802199286026c975c93cc470b
6
+ metadata.gz: 7db1e43070c41e05c211962fc7d720b3cecc9ba681f8302b8ba6daae29b6fa94390adff0863970b394a9032b45471cb231227ecd2670039e83f2799884ec6be4
7
+ data.tar.gz: 850f23a3ae73ab859cff8540110f668e5b54d06757bb6954fed09a61f7c681ea7114c292f755d50f3563318b184d5e912a533b90b155f47aecbedc09c9a51b22
data/.cane ADDED
@@ -0,0 +1,4 @@
1
+ --no-doc
2
+ --style-measure 99
3
+ --abc-max 5
4
+ --style-exclude spec/**/*
data/.gitignore CHANGED
@@ -4,3 +4,5 @@ Gemfile*.lock
4
4
  pkg/*
5
5
  spec/test.sqlite3
6
6
  coverage
7
+ .idea
8
+ gemfiles/*.lock
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ Include:
3
+ - '**/Rakefile'
4
+ Exclude:
5
+ - 'spec/**/*'
6
+ Metrics/LineLength:
7
+ Max: 99
8
+ Style/FileName:
9
+ Enabled: false
10
+ Style/ModuleFunction:
11
+ Enabled: false
12
+ Style/Encoding:
13
+ Enabled: false
14
+ Documentation:
15
+ Enabled: false
16
+ Metrics/MethodLength:
17
+ Max: 15
@@ -1,11 +1,19 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 1.9.2
5
3
  - 2.0.0
4
+ - 2.1
5
+ - 2.2
6
6
  - ruby-head
7
7
  - jruby-19mode # JRuby in 1.9 mode
8
- - rbx-19mode
8
+ - rbx-2
9
9
  gemfile:
10
- - gemfiles/Gemfile.rails-3.2.x
11
- - Gemfile
10
+ - gemfiles/activerecord_3.2.gemfile
11
+ - gemfiles/activerecord_4.0.gemfile
12
+ - gemfiles/activerecord_4.1.gemfile
13
+ - gemfiles/activerecord_4.2.gemfile
14
+ - gemfiles/activerecord_head.gemfile
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: ruby-head
18
+ - rvm: jruby-19mode
19
+ bundler_args: --without local_development
@@ -0,0 +1,24 @@
1
+ appraise 'activerecord-3.2' do
2
+ gem 'activerecord', '~> 3.2.0'
3
+ end
4
+
5
+ appraise 'activerecord-4.0' do
6
+ gem 'activerecord', '~> 4.0.0'
7
+ end
8
+
9
+ appraise 'activerecord-4.1' do
10
+ gem 'activerecord', '~> 4.1.0'
11
+ end
12
+
13
+ appraise 'activerecord-4.2' do
14
+ gem 'activerecord', '~> 4.2.0'
15
+ end
16
+
17
+ appraise 'activerecord-head' do
18
+ git 'git://github.com/rails/arel.git' do
19
+ gem 'arel'
20
+ end
21
+ git 'git://github.com/rails/rails.git' do
22
+ gem 'activerecord'
23
+ end
24
+ end
data/Gemfile CHANGED
@@ -1,10 +1,12 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in power_dup.gemspec
1
+ source 'https://rubygems.org'
4
2
  gemspec
5
- gem 'activerecord', '~> 4.1.2'
6
3
 
7
4
  group :development, :test do
8
5
  gem 'rake'
9
6
  gem 'coveralls', require: false
10
7
  end
8
+
9
+ group :local_development do
10
+ gem 'pry'
11
+ gem 'appraisal'
12
+ end
data/README.md CHANGED
@@ -1,12 +1,8 @@
1
- # IMPORTANT NEWS
1
+ # Amoeba
2
2
 
3
- * As of December 11th, 2012 Amoeba no longer overrides the built in `ActiveRecord::Base#dup` method and instead implements its own method called `amoeba_dup`. Make sure to update your code if you do a bundle update and or double check that your gemfile has a version restriction bound to v1.x e.g. '~> 1.2' listed for amoeba.
3
+ Easy cloning of active_record objects including associations and several operations under associations and attributes.
4
4
 
5
- ## Amoeba
6
-
7
- Easy copying of rails associations such as `has_many`.
8
-
9
- ![amoebalogo](http://rocksolidwebdesign.com/wp_cms/wp-content/uploads/2012/02/amoeba_logo.jpg)
5
+ [![Code Climate](https://codeclimate.com/github/rocksolidwebdesign/amoeba/badges/gpa.svg)](https://codeclimate.com/github/rocksolidwebdesign/amoeba)
10
6
 
11
7
  ## What?
12
8
 
@@ -18,7 +14,7 @@ This gem is named "Amoeba" because amoebas are (small life forms that are) good
18
14
 
19
15
  An ActiveRecord extension gem to allow the duplication of associated child record objects when duplicating an active record model.
20
16
 
21
- Rails 3.2 compatible.
17
+ Rails 3.2, 4.x compatible.
22
18
 
23
19
  ### Features
24
20
 
@@ -116,8 +112,8 @@ If you only want some of the associations copied but not others, you may use the
116
112
 
117
113
  amoeba do
118
114
  enable
119
- include_field :tags
120
- include_field :authors
115
+ include_association :tags
116
+ include_association :authors
121
117
  end
122
118
  end
123
119
 
@@ -133,8 +129,8 @@ Using the inclusive style within the amoeba block actually implies that you wish
133
129
  has_many :authors
134
130
 
135
131
  amoeba do
136
- include_field :tags
137
- include_field :authors
132
+ include_association :tags
133
+ include_association :authors
138
134
  end
139
135
  end
140
136
 
@@ -142,7 +138,7 @@ Using the inclusive style within the amoeba block actually implies that you wish
142
138
  belongs_to :post
143
139
  end
144
140
 
145
- You may also specify fields to be copied by passing an array. If you call the `include_field` with a single value, it will be appended to the list of already included fields. If you pass an array, your array will overwrite the original values.
141
+ You may also specify fields to be copied by passing an array. If you call the `include_association` with a single value, it will be appended to the list of already included fields. If you pass an array, your array will overwrite the original values.
146
142
 
147
143
  class Post < ActiveRecord::Base
148
144
  has_many :comments
@@ -150,7 +146,7 @@ You may also specify fields to be copied by passing an array. If you call the `i
150
146
  has_many :authors
151
147
 
152
148
  amoeba do
153
- include_field [:tags, :authors]
149
+ include_association [:tags, :authors]
154
150
  end
155
151
  end
156
152
 
@@ -172,7 +168,7 @@ If you have more fields to include than to exclude, you may wish to shorten the
172
168
  has_many :authors
173
169
 
174
170
  amoeba do
175
- exclude_field :comments
171
+ exclude_association :comments
176
172
  end
177
173
  end
178
174
 
@@ -182,7 +178,7 @@ If you have more fields to include than to exclude, you may wish to shorten the
182
178
 
183
179
  This example does the same thing as the inclusive style example, it will copy the post's tags and authors but not its comments. As with inclusive style, there is no need to explicitly enable amoeba when specifying fields to exclude.
184
180
 
185
- The exclusive style, when used, will automatically disable any other style that was previously selected, so if you selected include fields, and then you choose some exclude fields, the `exclude_field` method will disable the previously slected inclusive style and wipe out any corresponding include fields.
181
+ The exclusive style, when used, will automatically disable any other style that was previously selected, so if you selected include fields, and then you choose some exclude fields, the `exclude_association` method will disable the previously slected inclusive style and wipe out any corresponding include fields.
186
182
 
187
183
  #### Cloning
188
184
 
@@ -218,7 +214,7 @@ This example will actually duplicate the warnings and widgets in the database. I
218
214
 
219
215
  #### Limiting Association Types
220
216
 
221
- By default, amoeba recognizes and attemps to copy any children of the following association types:
217
+ By default, amoeba recognizes and attempts to copy any children of the following association types:
222
218
 
223
219
  - has one
224
220
  - has many
@@ -346,7 +342,7 @@ or this, using an array:
346
342
  has_and_belongs_to_many :tags
347
343
 
348
344
  amoeba do
349
- include_field :tags
345
+ include_association :tags
350
346
 
351
347
  customize([
352
348
  lambda do |orig_obj,copy_of_obj|
@@ -413,11 +409,11 @@ This example should result in something like this:
413
409
  new_post.title # "Copy of Hello world"
414
410
  new_post.contents # "Original contents: I like cats, cats are awesome. (copied version)"
415
411
 
416
- Like `nullify`, the preprocessing directives do not automatically enable the copying of associated child records. If only preprocessing directives are used and you do want to copy child records and no `include_field` or `exclude_field` list is provided, you must still explicitly enable the copying of child records by calling the enable method from within the amoeba block on your model.
412
+ Like `nullify`, the preprocessing directives do not automatically enable the copying of associated child records. If only preprocessing directives are used and you do want to copy child records and no `include_association` or `exclude_association` list is provided, you must still explicitly enable the copying of child records by calling the enable method from within the amoeba block on your model.
417
413
 
418
414
  ### Precedence
419
415
 
420
- You may use a combination of configuration methods within each model's amoeba block. Recognized association types take precedence over inclusion or exclusion lists. Inclusive style takes precedence over exclusive style, and these two explicit styles take precedence over the indiscriminate style. In other words, if you list fields to copy, amoeba will only copy the fields you list, or only copy the fields you don't exclude as the case may be. Additionally, if a field type is not recognized it will not be copied, regardless of whether it appears in an inclusion list. If you want amoeba to automatically copy all of your child records, do not list any fields using either `include_field` or `exclude_field`.
416
+ You may use a combination of configuration methods within each model's amoeba block. Recognized association types take precedence over inclusion or exclusion lists. Inclusive style takes precedence over exclusive style, and these two explicit styles take precedence over the indiscriminate style. In other words, if you list fields to copy, amoeba will only copy the fields you list, or only copy the fields you don't exclude as the case may be. Additionally, if a field type is not recognized it will not be copied, regardless of whether it appears in an inclusion list. If you want amoeba to automatically copy all of your child records, do not list any fields using either `include_association` or `exclude_association`.
421
417
 
422
418
  The following example syntax is perfectly valid, and will result in the usage of inclusive style. The order in which you call the configuration methods within the amoeba block does not matter:
423
419
 
@@ -432,13 +428,13 @@ The following example syntax is perfectly valid, and will result in the usage of
432
428
  has_many :authors
433
429
 
434
430
  amoeba do
435
- exclude_field :authors
436
- include_field :tags
431
+ exclude_association :authors
432
+ include_association :tags
437
433
  nullify :date_published
438
434
  prepend :title => "Copy of "
439
435
  append :contents => " (copied version)"
440
436
  regex :contents => {:replace => /dog/, :with => 'cat'}
441
- include_field :authors
437
+ include_association :authors
442
438
  enable
443
439
  nullify :topic_id
444
440
  end
@@ -450,7 +446,7 @@ The following example syntax is perfectly valid, and will result in the usage of
450
446
 
451
447
  This example will copy all of a post's tags and authors, but not its comments. It will also nullify the publishing date and dissociate the post from its original topic. It will also preprocess the post's fields as in the previous preprocessing example.
452
448
 
453
- Note that, because of precedence, inclusive style is used and the list of exclude fields is never consulted. Additionally, the `enable` method is redundant because amoeba is automatically enabled when using `include_field`.
449
+ Note that, because of precedence, inclusive style is used and the list of exclude fields is never consulted. Additionally, the `enable` method is redundant because amoeba is automatically enabled when using `include_association`.
454
450
 
455
451
  The preprocessing directives are run after child records are copied andare run in this order.
456
452
 
@@ -646,18 +642,18 @@ The `:relaxed` parenting style will prefer parent settings.
646
642
  has_and_belongs_to_many :sections
647
643
 
648
644
  amoeba do
649
- exclude_field :images
645
+ exclude_association :images
650
646
  propagate :relaxed
651
647
  end
652
648
  end
653
649
 
654
650
  class Shirt < Product
655
- include_field :images
656
- include_field :sections
651
+ include_association :images
652
+ include_association :sections
657
653
  prepend :title => "Copy of "
658
654
  end
659
655
 
660
- In this example, the conflicting `include_field` settings on the child will be ignored and the parent `exclude_field` setting will be used, while the `prepend` setting on the child will be honored because it doesn't conflict with the parent.
656
+ In this example, the conflicting `include_association` settings on the child will be ignored and the parent `exclude_association` setting will be used, while the `prepend` setting on the child will be honored because it doesn't conflict with the parent.
661
657
 
662
658
  ##### Strict Parenting
663
659
 
@@ -668,18 +664,18 @@ The `:strict` style will ignore child settings altogether and inherit any parent
668
664
  has_and_belongs_to_many :sections
669
665
 
670
666
  amoeba do
671
- exclude_field :images
667
+ exclude_association :images
672
668
  propagate :strict
673
669
  end
674
670
  end
675
671
 
676
672
  class Shirt < Product
677
- include_field :images
678
- include_field :sections
673
+ include_association :images
674
+ include_association :sections
679
675
  prepend :title => "Copy of "
680
676
  end
681
677
 
682
- In this example, the only processing that will happen when a Shirt is duplicated is whatever processing is allowed by the parent. So in this case the parent's `exclude_field` directive takes precedence over the child's `include_field` settings, and not only that, but none of the other settings for the child are used either. The `prepend` setting of the child is completely ignored.
678
+ In this example, the only processing that will happen when a Shirt is duplicated is whatever processing is allowed by the parent. So in this case the parent's `exclude_association` directive takes precedence over the child's `include_association` settings, and not only that, but none of the other settings for the child are used either. The `prepend` setting of the child is completely ignored.
683
679
 
684
680
  ##### Parenting and Precedence
685
681
 
@@ -704,13 +700,13 @@ This version will use both the parent and child settings, so both the images and
704
700
  has_and_belongs_to_many :sections
705
701
 
706
702
  amoeba do
707
- include_field :images
703
+ include_association :images
708
704
  propagate
709
705
  end
710
706
  end
711
707
 
712
708
  class Shirt < Product
713
- include_field :sections
709
+ include_association :sections
714
710
  end
715
711
 
716
712
  The next version will use only the child settings because passing an array will override any previous settings rather than adding to them and the child config takes precedence in the `submissive` parenting style. So in this case only the sections will be copied.
@@ -720,13 +716,13 @@ The next version will use only the child settings because passing an array will
720
716
  has_and_belongs_to_many :sections
721
717
 
722
718
  amoeba do
723
- include_field :images
719
+ include_association :images
724
720
  propagate
725
721
  end
726
722
  end
727
723
 
728
724
  class Shirt < Product
729
- include_field [:sections]
725
+ include_association [:sections]
730
726
  end
731
727
 
732
728
  ##### A Relaxed Override Example
@@ -738,13 +734,13 @@ This version will use both the parent and child settings, so both the images and
738
734
  has_and_belongs_to_many :sections
739
735
 
740
736
  amoeba do
741
- include_field :images
737
+ include_association :images
742
738
  propagate :relaxed
743
739
  end
744
740
  end
745
741
 
746
742
  class Shirt < Product
747
- include_field :sections
743
+ include_association :sections
748
744
  end
749
745
 
750
746
  The next version will use only the parent settings because passing an array will override any previous settings rather than adding to them and the parent config takes precedence in the `relaxed` parenting style. So in this case only the images will be copied.
@@ -754,13 +750,13 @@ The next version will use only the parent settings because passing an array will
754
750
  has_and_belongs_to_many :sections
755
751
 
756
752
  amoeba do
757
- include_field [:images]
753
+ include_association [:images]
758
754
  propagate
759
755
  end
760
756
  end
761
757
 
762
758
  class Shirt < Product
763
- include_field :sections
759
+ include_association :sections
764
760
  end
765
761
 
766
762
  ### Validating Nested Attributes
@@ -791,7 +787,7 @@ For example this will throw a validation error saying that your posts are invali
791
787
 
792
788
  author.save # this will fail validation
793
789
 
794
- Wheras this will work fine:
790
+ Where this will work fine:
795
791
 
796
792
  class Author < ActiveRecord::Base
797
793
  has_many :posts, :inverse_of => :author
@@ -847,21 +843,102 @@ This issue with `accepts_nested_attributes_for` can also be solved by using `:in
847
843
 
848
844
  The crux of the issue is that upon duplication, the new `Author` instance does not yet have an ID because it has not yet been persisted, so the `:posts` do not yet have an `:author_id` either, and thus no `:author` and thus they will fail validation. This issue may likely affect amoeba usage so if you get some validation failures, be sure to add `:inverse_of` to your models.
849
845
 
846
+
847
+ ## Cloning using custom method
848
+
849
+ If you need to clone model with custom method you can use `through`:
850
+
851
+ class ChildPrototype < ActiveRecord::Base
852
+ amoeba do
853
+ through :become_child
854
+ end
855
+
856
+ def become_child
857
+ self.dup.becomes(Child)
858
+ end
859
+ end
860
+
861
+ class Child < ChildPrototype
862
+ end
863
+
864
+ After cloning we will get instance of `Child` instead of `ChildPrototype`
865
+
866
+ ## Remapping associations
867
+
868
+ If you will need to do complex cloning with remapping associations name you can user `remapper`:
869
+
870
+ class ObjectPrototype < ActiveRecord::Base
871
+ has_many :child_prototypes
872
+
873
+ amoeba do
874
+ method :become_real
875
+ remapper :remap_associations
876
+ end
877
+
878
+ def become_real
879
+ self.dup().becomes( RealObject )
880
+ end
881
+
882
+ def remap_associations( name )
883
+ :childs if name == :child_prototypes
884
+ end
885
+ end
886
+
887
+ class RealObject < ObjectPrototype
888
+ has_many :childs
889
+ end
890
+
891
+ class ChildPrototype < ActiveRecord::Base
892
+ amoeba do
893
+ method :become_child
894
+ end
895
+
896
+ def become_child
897
+ self.dup().becomes( Child )
898
+ end
899
+ end
900
+
901
+ class Child < ChildPrototype
902
+ end
903
+
904
+ In result we will get next:
905
+
906
+ prototype = ObjectPrototype.new
907
+ prototype.child_prototypes << ChildPrototype.new
908
+ object = prototype.amoeba_dup
909
+ object.class # => RealObject
910
+ object.childs.first.class #=> Child
911
+
850
912
  ## Configuration Reference
851
913
 
852
914
  Here is a static reference to the available configuration methods, usable within the amoeba block on your rails models.
853
915
 
916
+ ### through
917
+
918
+ Set method what we will use for cloning model instead of `dup`.
919
+
920
+ for example:
921
+
922
+ amoeba do
923
+ through :supper_pupper_dup
924
+ end
925
+
926
+ def supper_pupper_dup
927
+ puts "multiplied by budding"
928
+ self.dup
929
+ end
930
+
854
931
  ### Controlling Associations
855
932
 
856
933
  #### enable
857
934
 
858
- Enables amoeba in the default style of copying all known associated child records. Using the enable method is only required if you wish to enable amoeba but you are not using either the `include_field` or `exclude_field` directives. If you use either inclusive or exclusive style, amoeba is automatically enabled for you, so calling `enable` would be redundant, though it won't hurt.
935
+ Enables amoeba in the default style of copying all known associated child records. Using the enable method is only required if you wish to enable amoeba but you are not using either the `include_association` or `exclude_association` directives. If you use either inclusive or exclusive style, amoeba is automatically enabled for you, so calling `enable` would be redundant, though it won't hurt.
859
936
 
860
- #### include_field
937
+ #### include_association
861
938
 
862
939
  Adds a field to the list of fields which should be copied. All associations not in this list will not be copied. This method may be called multiple times, once per desired field, or you may pass an array of field names. Passing a single symbol will add to the list of included fields. Passing an array will empty the list and replace it with the array you pass.
863
940
 
864
- #### exclude_field
941
+ #### exclude_association
865
942
 
866
943
  Adds a field to the list of fields which should not be copied. Only the associations that are not in this list will be copied. This method may be called multiple times, once per desired field, or you may pass an array of field names. Passing a single symbol will add to the list of excluded fields. Passing an array will empty the list and replace it with the array you pass.
867
944
 
@@ -897,6 +974,19 @@ Here is a static reference to the available configuration methods, usable within
897
974
 
898
975
  will choose the relaxed parenting style of inherited settings for this child. A parenting style set via the `raised` method takes precedence over the parenting style set using the `propagate` method.
899
976
 
977
+ #### remapper
978
+
979
+ Set the method what will be used for remapping of association name. Method will have one argument - association name as Symbol. If method will return nil then association will not be remapped.
980
+
981
+ for example:
982
+ amoeba do
983
+ remapper :childs_to_parents
984
+ end
985
+
986
+ def childs_to_parents(association_name)
987
+ :parents if association_name == :childs
988
+ end
989
+
900
990
  ### Pre-Processing Fields
901
991
 
902
992
  #### nullify