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
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/alchemy/essence.rb', line 39 def acts_as_essence( = {}) register_as_essence_association! configuration = { ingredient_column: "body", }.update() @_classes_with_ingredient_association ||= [] 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.available) } 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_save :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 @_classes_with_ingredient_association << self end end |