Method: Alchemy::Essence::ClassMethods#acts_as_essence

Defined in:
lib/alchemy/essence.rb

#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