Module: Draftsman::Model::ClassMethods
- Defined in:
- lib/draftsman/model.rb
Instance Method Summary collapse
-
#draft_class ⇒ Object
Returns draft class.
-
#draftable? ⇒ Boolean
Returns whether or not ‘has_drafts` has been called on this model.
-
#has_drafts(options = {}) ⇒ Object
Declare this in your model to enable the Draftsman API for it.
-
#trashable? ⇒ Boolean
Returns whether or not a ‘trashed_at` timestamp is set up on this model.
Instance Method Details
#draft_class ⇒ Object
Returns draft class.
122 123 124 |
# File 'lib/draftsman/model.rb', line 122 def draft_class @draft_class ||= draft_class_name.constantize end |
#draftable? ⇒ Boolean
Returns whether or not ‘has_drafts` has been called on this model.
127 128 129 |
# File 'lib/draftsman/model.rb', line 127 def draftable? method_defined?(:draftsman_options) end |
#has_drafts(options = {}) ⇒ Object
Declare this in your model to enable the Draftsman API for it. A draft of the model is available in the ‘draft` association (if one exists).
Options:
:class_name The name of a custom ‘Draft` class. This class should inherit from `Draftsman::Draft`. A global default can be set for this using `Draftsman.draft_class_name=` if the default of `Draftsman::Draft` needs to be overridden.
:ignore An array of attributes for which an update to a ‘Draft` will not be stored if they are the only ones changed.
:only Inverse of ‘ignore` - a new `Draft` will be created only for these attributes if supplied. It’s recommended that you only specify optional attributes for this (that can be empty).
:skip Fields to ignore completely. As with ‘ignore`, updates to these fields will not create a new `Draft`. In addition, these fields will not be included in the serialized versions of the object whenever a new `Draft` is created.
:meta A hash of extra data to store. You must add a column to the ‘drafts` table for each key. Values are objects or `procs` (which are called with `self`, i.e. the model with the `has_drafts`). See `Draftsman::Controller.info_for_draftsman` for an example of how to store data from the controller.
:draft The name to use for the ‘draft` association shortcut method. Default is `:draft`.
:published_at The name to use for the method which returns the published timestamp. Default is ‘published_at`.
:trashed_at The name to use for the method which returns the soft delete timestamp. Default is ‘trashed_at`.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/draftsman/model.rb', line 55 def has_drafts( = {}) # Lazily include the instance methods so we don't clutter up # any more ActiveRecord models than we need to. send :include, InstanceMethods send :extend, AttributesSerialization # Define before/around/after callbacks on each drafted model send :extend, ActiveModel::Callbacks # TODO: Remove `draft_creation`, `draft_update`, and `draft_destroy` in # v1.0. define_model_callbacks :save_draft, :draft_creation, :draft_update, :draft_destruction, :draft_destroy class_attribute :draftsman_options self. = .dup class_attribute :draft_association_name self.draft_association_name = [:draft] || :draft class_attribute :draft_class_name self.draft_class_name = [:class_name] || Draftsman.draft_class_name [:ignore, :skip, :only].each do |key| [key] = ([[key]].flatten.compact || []).map(&:to_s) end [:ignore] << "#{self.draft_association_name}_id" [:meta] ||= {} attr_accessor :draftsman_event class_attribute :published_at_attribute_name self.published_at_attribute_name = [:published_at] || :published_at class_attribute :trashed_at_attribute_name self.trashed_at_attribute_name = [:trashed_at] || :trashed_at # `belongs_to :draft` association if ::Draftsman.active_record_belongs_to_required? belongs_to(self.draft_association_name, class_name: self.draft_class_name, dependent: :destroy, optional: true) else belongs_to(self.draft_association_name, class_name: self.draft_class_name, dependent: :destroy) end # Scopes scope :drafted, -> (referenced_table_name = nil) { referenced_table_name = referenced_table_name.present? ? referenced_table_name : table_name where.not(referenced_table_name => { "#{self.draft_association_name}_id" => nil }) } scope :published, -> (referenced_table_name = nil) { referenced_table_name = referenced_table_name.present? ? referenced_table_name : table_name where.not(referenced_table_name => { self.published_at_attribute_name => nil }) } scope :trashed, -> (referenced_table_name = nil) { referenced_table_name = referenced_table_name.present? ? referenced_table_name : table_name where.not(referenced_table_name => { self.trashed_at_attribute_name => nil }) } scope :live, -> (referenced_table_name = nil) { referenced_table_name = referenced_table_name.present? ? referenced_table_name : table_name where(referenced_table_name => { self.trashed_at_attribute_name => nil }) } end |
#trashable? ⇒ Boolean
Returns whether or not a ‘trashed_at` timestamp is set up on this model.
132 133 134 |
# File 'lib/draftsman/model.rb', line 132 def trashable? draftable? && method_defined?(self.trashed_at_attribute_name) end |