Class: Cortex::ContentItem

Inherits:
ApplicationRecord show all
Includes:
ActiveModel::Transitions, BelongsToTenant, SearchableContentItem
Defined in:
app/models/cortex/content_item.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

FieldItem and State Convenience Methods. TODO: move to concern? transactions?



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/cortex/content_item.rb', line 39

def method_missing(method_name, *arguments, &block)
  super unless dynamic_method?(method_name)

  if dynamic_state_check?(method_name)
    # Used to check state - allows for methods such as #published? and #expired?
    # Will return true if the active_state corresponds to the name of the method
    "#{publish_state.downcase}?" == method_name.to_s
  else
    # Used to query for any field on the relevant ContentType and return data from the content_item
    field_items.find { |field_item| field_item.field.name.parameterize(separator: '_') == method_name.to_s }.data.values.first
  end
end

Instance Method Details

#publish_stateObject



34
35
36
# File 'app/models/cortex/content_item.rb', line 34

def publish_state
  GetPublishStateTransaction.new.call(self).value!
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/cortex/content_item.rb', line 52

def respond_to_missing?(method_name, include_private = false)
  dynamic_method?(method_name) || super
end