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