Sha256: 61f2dfdb4e66755ffb2569663c9a7f1d7a8965868f5e6306e43a74eb204fc29f

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

module Locomotive::Steam

  class ContentTypeField

    include Locomotive::Steam::Models::Entity

    attr_accessor :content_type

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

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

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

    def order_by
      if (order_by = self[:order_by]).present?
        name, direction = order_by.split
        { name.to_sym => direction || 'asc' }
      else
        type == :has_many ? { :"position_in_#{self[:inverse_of]}" => 'asc' } : nil
      end
    end

    alias :target :class_name

    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

    def required?; self[:required]; end
    def localized?; self[:localized]; end

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

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

    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
      else name
      end
    end

    class SelectOption

      include Locomotive::Steam::Models::Entity

      attr_accessor :field

      def name
        self[:name]
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/entities/content_type_field.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/entities/content_type_field.rb