Module: ActiveRecord::DraftRecords
- Defined in:
- lib/active_record/draft_records.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#save_and_attempt_to_undraft(options = {}) ⇒ Object
Save the record and, if it is a draft, attempt to transform it in a normal (non-draft) record.
-
#save_as_draft ⇒ Object
Save the record as a draft, setting the record attribute ‘draft’ to true and saving the record ignoring the validations.
-
#save_or_draft(options = {}) ⇒ Object
Attempt to save the record, if any validation fails, save it as a draft.
-
#valid?(context = nil) ⇒ Boolean
Override valid? to automatically and temporarily set it as not a draft for the validation.
Class Method Details
.included(klass) ⇒ Object
3 4 5 6 7 |
# File 'lib/active_record/draft_records.rb', line 3 def self.included(klass) klass.extend(ClassMethods) # Define default_scope to not include drafts klass.send(:default_scope, :conditions => {:draft => false}) end |
Instance Method Details
#save_and_attempt_to_undraft(options = {}) ⇒ Object
Save the record and, if it is a draft, attempt to transform it in a normal (non-draft) record.
50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/draft_records.rb', line 50 def save_and_attempt_to_undraft( = {}) if self.draft? self.draft = false if self.valid? save(.merge(:validate => false)) !self.draft else save end end |
#save_as_draft ⇒ Object
Save the record as a draft, setting the record attribute ‘draft’ to true and saving the record ignoring the validations.
62 63 64 65 |
# File 'lib/active_record/draft_records.rb', line 62 def save_as_draft self.draft = true save(:validate => false) end |
#save_or_draft(options = {}) ⇒ Object
Attempt to save the record, if any validation fails, save it as a draft.
68 69 70 |
# File 'lib/active_record/draft_records.rb', line 68 def save_or_draft( = {}) save() || save_as_draft end |
#valid?(context = nil) ⇒ Boolean
Override valid? to automatically and temporarily set it as not a draft for the validation.
42 43 44 45 46 47 |
# File 'lib/active_record/draft_records.rb', line 42 def valid?(context = nil) self.draft, is_draft = false, self.draft super ensure self.draft = is_draft end |