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.


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
68
69
70
71
72
73
74
75
76
77
# File 'lib/alchemy/essence.rb', line 27

def acts_as_essence(options = {})
  register_as_essence_association!

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

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    attr_writer :validation_errors
    include Alchemy::Essence::InstanceMethods

    validate :validate_ingredient, on: :update, if: -> { validations.any? }

    has_one :content, as: :essence, class_name: "Alchemy::Content", inverse_of: :essence
    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.published) }
    scope :from_element, ->(name) { joins(:element).where(Element.table_name => { name: name }) }

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

    after_update :touch_element

    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
  RUBY

  if configuration[:belongs_to]
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      belongs_to :ingredient_association, **#{configuration[:belongs_to]}

      alias_method :#{configuration[:ingredient_column]}, :ingredient_association
      alias_method :#{configuration[:ingredient_column]}=, :ingredient_association=
    RUBY
  end
end