forked from caseydreier/dirty_associations
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding collection module for creating dirty association collection me…
…thods.
- Loading branch information
1 parent
595672f
commit dfbc4f7
Showing
6 changed files
with
80 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module DirtyAssociations | ||
class Base | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module DirtyAssociations | ||
module CollectionMethods | ||
|
||
# Creates methods for dirty collections associations | ||
def generate_collection_methods! | ||
generate_collection_object_methods! | ||
generate_collection_id_methods! | ||
end | ||
|
||
def generate_collection_id_methods! | ||
base.instance_eval <<-EOV | ||
def #{association_name_singular}_ids_were; (original_associations["#{association_name_singular}_original_ids".to_sym] || []).uniq; end; | ||
def #{association_name_singular}_ids_removed(); #{association_name_singular}_ids_were - #{association_name_singular}_ids; end; | ||
def #{association_name_singular}_ids_removed?(); !#{association_name_singular}_ids_removed.empty?; end; | ||
def #{association_name_singular}_ids_added(); #{association_name_singular}_ids - #{association_name_singular}_ids_were; end; | ||
def #{association_name_singular}_ids_added?(); !#{association_name_singular}_ids_added.empty?; end; | ||
def #{association_name_singular}_ids_changed?(); #{association_name_singular}_ids_added? || #{association_name_singular}_ids_removed?; end; | ||
EOV | ||
end | ||
|
||
def generate_collection_object_methods! | ||
base.instance_eval <<-EOV | ||
def #{association_name}_were; (original_associations["#{association_name_singular}_original_ids".to_sym] || []).uniq; end; | ||
def #{association_name}_removed(); #{association_name_singular}_ids_were - #{association_name_singular}_ids; end; | ||
def #{association_name}_removed?(); !#{association_name_singular}_ids_removed.empty?; end; | ||
def #{association_name}_added(); #{association_name_singular}_ids - #{association_name}_ids_were; end; | ||
def #{association_name}_added?(); !#{association_name_singular}_ids_added.empty?; end; | ||
def #{association_name}_changed?(); #{association_name_singular}_ids_added? || #{association_name_singular}_ids_removed?; end; | ||
EOV | ||
|
||
end | ||
|
||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters