-
-
Save chrisbranson/9839239 to your computer and use it in GitHub Desktop.
Revisions
-
jopotts revised this gist
Jan 30, 2014 . 2 changed files with 12 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,12 @@ module DefaultValues def has_default_values(default_values = {}) cattr_accessor :default_values self.default_values = default_values after_initialize :assign_default_values include InstanceMethods end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,11 @@ # Example usage class SomeModel < ActiveRecord::Base has_default_values( title: "Default", flag_yn: false ) .. end -
jopotts created this gist
Nov 4, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ module DefaultValues def has_default_values after_initialize :assign_default_values include InstanceMethods end module InstanceMethods private def assign_default_values return unless new_record? default_values.each do |key, value| self[key] = value if self[key].nil? end end end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ # In config/initializers ActiveRecord::Base.extend DefaultValues This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ # Example usage class SomeModel < ActiveRecord::Base has_default_values private def default_values { title: "Default", flag_yn: false } end end