Sha256: 5fd50dd5bdd2a5eb8679474c8523c1e94bbabf728dbdc613317e868249189a49

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

module Locomotive
  module Steam

    class ContentTypeFieldRepository

      include Models::Repository

      attr_accessor :content_type

      # Entity mapping
      mapping :content_type_fields, entity: ContentTypeField do
        default_attribute :content_type, -> (repository) { repository.content_type }

        embedded_association :select_options, ContentTypeFieldSelectOptionRepository
      end

      def by_name(name)
        first { where(name: name) }
      end

      def selects
        query { where(type: :select) }.all
      end

      def belongs_to
        query { where(type: :belongs_to) }.all
      end

      def many_to_many
        query { where(type: :many_to_many) }.all
      end

      def associations
        query { where(k(:type, :in) => %i(belongs_to has_many many_to_many)) }.all
      end

      def no_associations
        query { where(k(:type, :nin) => %i(belongs_to has_many many_to_many)) }.all
      end

      def unique
        query { where(unique: true) }.all.inject({}) do |memo, field|
          memo[field.name] = field
          memo
        end
      end

      def required
        query { where(required: true) }.all
      end

      def localized_names
        query { where(localized: true) }.all.map(&:name)
      end

      def select_options(name)
        if field = first { where(name: name, type: :select) }
          field.select_options.all
        else
          nil
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0 lib/locomotive/steam/repositories/content_type_field_repository.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/repositories/content_type_field_repository.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/repositories/content_type_field_repository.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/repositories/content_type_field_repository.rb