Sha256: 734c33c7564768c95a2652e64991a420b593b8db8fc0e984036a81de4c855ae4

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 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 generate_yaml
        Typus.reload!

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

        models.each do |model|
          configuration[model.table_name] = {}

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

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

          configuration[model.table_name][:base] = <<-RAW
#{model}:
  fields:
    default: #{fields}
    form: #{fields}
  relationships: #{relationships}
  application: Application
          RAW

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

        configuration
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
typus-3.1.2 lib/generators/typus/config_generator.rb
typus-3.1.1 lib/generators/typus/config_generator.rb
typus-3.1.0 lib/generators/typus/config_generator.rb