Sha256: 0832b2104277b8a0c93b8f8c2bc1adf81e405d690993f48277965f482918c5f7

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

module Typus
  module Generators
    class ConfigGenerator < Rails::Generators::Base

      source_root File.expand_path("../../templates", __FILE__)

      desc <<-MSG
Description:
  Creates configuration files.

      MSG

      def generate_config
        copy_file "config/typus/README"
        generate_yaml.each do |key, value|
          if (@configuration = value)[:base].present?
            template "config/typus/application.yml", "config/typus/#{key}.yml"
            template "config/typus/application_roles.yml", "config/typus/#{key}_roles.yml"
          end
        end
      end

      protected

      def configuration
        @configuration
      end

      def fields_for(model, *defaults)
        rejections = %w( ^id$ _type$ type created_at created_on updated_at updated_on deleted_at ).join("|")
        fields = model.table_exists? ? model.columns.map(&:name) : defaults
        fields.reject { |f| f.match(rejections) }.join(", ")
      end

      def generate_yaml
        Typus.reload!

        configuration = {}
        models = Typus.application_models.reject { |m| Typus.models.include?(m) }.map(&:constantize)

        models.each do |model|
          key = model.name.underscore

          configuration[key] = {}

          relationships = [ :has_many, :has_one ].map do |relationship|
                            model.reflect_on_all_associations(relationship).map { |i| i.name.to_s }
                          end.flatten.join(", ")

          configuration[key][:base] = <<-RAW
#{model}:
  fields:
    default: #{fields_for(model, 'to_label')}
    form: #{fields_for(model)}
  relationships: #{relationships}
  application: Application
          RAW

          configuration[key][:roles] = "#{model}: create, read, update, delete"
        end

        configuration
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
brainsome_typus-4.0.0.beta13 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta12 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta11 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta10 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta9 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta8 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta7 lib/generators/typus/config_generator.rb
typus-rails4-4.0.0.beta6 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta6 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta5 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta4 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta3 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta2 lib/generators/typus/config_generator.rb
brainsome_typus-4.0.0.beta1 lib/generators/typus/config_generator.rb
typus-3.1.4 lib/generators/typus/config_generator.rb
typus-3.1.3 lib/generators/typus/config_generator.rb