Module: Alchemy::Essence::ClassMethods

Defined in:
lib/alchemy/essence.rb

Overview

Delivers various methods we need for Essences in Alchemy.

To turn a model into an essence call acts_as_essence inside your model and you will get:

* validations
* several getters (ie: page, element, content, ingredient, preview_text)

Instance Method Summary collapse

Instance Method Details

#acts_as_essence(options = {}) ⇒ Object

Turn any active record model into an essence by calling this class method

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • ingredient_column (String || Symbol) — default: 'body'

    specifies the column name you use for storing the content in the database (default: body)

  • validate_column (String || Symbol) — default: ingredient_column

    The column the the validations run against.

  • preview_text_column (String || Symbol) — default: ingredient_column

    Specify the column for the preview_text method.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/alchemy/essence.rb', line 25

def acts_as_essence(options = {})
  register_as_essence_association!

  configuration = {
    ingredient_column: 'body'
  }.update(options)

  class_eval <<-EOV
    attr_writer :validation_errors
    include Alchemy::Essence::InstanceMethods
    stampable stamper_class_name: Alchemy.user_class_name
    validate :validate_ingredient, :on => :update, :if => 'validations.any?'

    has_one :content, :as => :essence, class_name: "Alchemy::Content"
    has_one :element, :through => :content, class_name: "Alchemy::Element"
    has_one :page,    :through => :element, class_name: "Alchemy::Page"

    scope :available,    -> { joins(:element).merge(Alchemy::Element.available) }
    scope :from_element, ->(name) { joins(:element).where(Element.table_name => { name: name }) }

    delegate :restricted?, to: :page,    allow_nil: true
    delegate :trashed?,    to: :element, allow_nil: true
    delegate :public?,     to: :element, allow_nil: true

    after_update :touch_content

    def acts_as_essence_class
      #{name}
    end

    def ingredient_column
      '#{configuration[:ingredient_column]}'
    end

    def validation_column
      '#{configuration[:validate_column] || configuration[:ingredient_column]}'
    end

    def preview_text_column
      '#{configuration[:preview_text_column] || configuration[:ingredient_column]}'
    end
  EOV
end

#register_as_essence_association!Object

Register the current class as has_many association on Alchemy::Page and Alchemy::Element models



70
71
72
73
74
75
# File 'lib/alchemy/essence.rb', line 70

def register_as_essence_association!
  klass_name = model_name.to_s
  arguments = [:has_many, klass_name.demodulize.tableize.to_sym, through: :contents,
    source: :essence, source_type: klass_name]
  %w(Page Element).each { |k| "Alchemy::#{k}".constantize.send(*arguments) }
end