Class: Locomotive::Steam::ContentTypeField

Inherits:
Object
  • Object
show all
Includes:
Models::Entity
Defined in:
lib/locomotive/steam/entities/content_type_field.rb

Defined Under Namespace

Classes: SelectOption

Instance Attribute Summary collapse

Attributes included from Models::Entity

#associations, #attributes, #base_url, #localized_attributes

Instance Method Summary collapse

Methods included from Models::Entity

#[], #[]=, #_id, #change, #method_missing, #respond_to?, #serialize

Methods included from Models::Concerns::ToJson

#as_json, #to_hash, #to_json

Methods included from Models::Concerns::Validation

#errors, #valid?

Constructor Details

#initialize(attributes = {}) ⇒ ContentTypeField

Returns a new instance of ContentTypeField.



9
10
11
12
13
14
15
16
17
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 9

def initialize(attributes = {})
  super({
    type:       :string,
    localized:  false,
    required:   false,
    unique:     false,
    default:    nil
  }.merge(attributes))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Locomotive::Steam::Models::Entity

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



7
8
9
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 7

def content_type
  @content_type
end

Instance Method Details

#association_optionsObject



52
53
54
55
56
57
58
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 52

def association_options
  {
    target_id:  target_id,
    inverse_of: self[:inverse_of],
    order_by:   order_by
  }
end

#class_nameObject Also known as: target



23
24
25
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 23

def class_name
  self[:class_name] || self[:target]
end

#is_relationship?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 60

def is_relationship?
  %i(belongs_to has_many many_to_many).include?(self.type)
end

#localized?Boolean

Returns:

  • (Boolean)


50
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 50

def localized?; self[:localized]; end

#order_byObject



27
28
29
30
31
32
33
34
35
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 27

def order_by
  if (order_by = self[:order_by]).present?
    # from Filesystem -> string, from MongoDB -> array (string transformed by Engine)
    name, direction = order_by.respond_to?(:each) ? order_by : order_by.split
    { name.to_sym => direction || 'asc' }
  else
    type == :has_many ? { :"position_in_#{self[:inverse_of]}" => 'asc' } : nil
  end
end

#persisted_nameObject



64
65
66
67
68
69
70
71
72
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 64

def persisted_name
  case type
  when :belongs_to, :select then "#{name}_id"
  when :many_to_many        then "#{name.singularize}_ids"
  when :has_many            then nil
  when :password            then nil
  else name
  end
end

#required?Boolean

Returns:

  • (Boolean)


49
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 49

def required?; self[:required]; end

#target_idObject



39
40
41
42
43
44
45
46
47
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 39

def target_id
  return @target_id if @target_id

  @target_id = if self.target =~ Locomotive::Steam::CONTENT_ENTRY_ENGINE_CLASS_NAME
    $1
  else
    self.target
  end
end

#typeObject



19
20
21
# File 'lib/locomotive/steam/entities/content_type_field.rb', line 19

def type
  self[:type].try(:to_sym)
end