Sha256: d489efc6f05f5c7b875b2d303df5c07fb3054bff9a912998e3377397d0ed1e26

Contents?: true

Size: 1.74 KB

Versions: 51

Compression:

Stored size: 1.74 KB

Contents

require 'thor/group'
require 'ostruct'
require 'active_support'
require 'active_support/core_ext'
require 'faker'

# Remove I18n warnings (i18n lib is required by faker)
I18n.enforce_available_locales = false

module Locomotive
  module Wagon
    module Generators
      class ContentType < Thor::Group

        include Thor::Actions
        include Locomotive::Wagon::CLI::ForceColor

        argument :slug
        argument :fields
        argument :target_path

        def copy_sources
          directory('.', target_path, { recursive: true }, {
            name:   name,
            slug:   slug,
            fields: extract_fields(fields)
          })
        end

        def self.source_root
          File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'generators', 'content_type')
        end

        protected

        def name
          options['name'] || slug.humanize
        end

        def extract_fields(fields)
          fields.map do |raw_attributes|
            name, type, label, required, localized, target_content_type_slug  = raw_attributes.split(':')

            OpenStruct.new(
              name:       name,
              label:      label || name.humanize,
              type:       type || 'string',
              required:   %w(true required).include?(required),
              localized:  %w(true localized).include?(localized)
            ).tap do |field|
              if %w(belongs_to has_many many_to_many).include?(type)
                field.class_name = target_content_type_slug

                inverse_of = type == 'belongs_to' ? target_content_type_slug.singularize : target_content_type_slug

                field.inverse_of = inverse_of
              end
            end
          end
        end

      end

    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
locomotivecms_wagon-3.2.0.alpha2 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.2.0.alpha1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.1.1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.1.0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.1.0.beta1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.5 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.4 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.3 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.2 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.0.rc0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-3.0.0.beta2 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.4.1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.4.0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.4.0.rc2 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.4.0.rc1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.3.0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.3.0.rc1 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/generators/content_type.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/generators/content_type.rb