Class: Infold::DecoratorGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/infold/decorator_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_association_model_fileObject



24
25
26
27
28
29
30
# File 'lib/generators/infold/decorator_generator.rb', line 24

def create_association_model_file
  @resource.associations&.
    select { |as| !as.belongs_to? && as.field_group.has_association_model? }&.each do |association|
    @writer = DecoratorWriter.new(association)
    template "decorator.rb", Rails.root.join("app/decorators/admin/#{association.model_name(:snake)}_decorator.rb"), force: true
  end
end

#create_model_fileObject



19
20
21
22
# File 'lib/generators/infold/decorator_generator.rb', line 19

def create_model_file
  @writer = DecoratorWriter.new(@resource)
  template "decorator.rb", Rails.root.join("app/decorators/admin/#{name.underscore.singularize}_decorator.rb"), force: true
end

#setupObject



11
12
13
14
15
16
17
# File 'lib/generators/infold/decorator_generator.rb', line 11

def setup
  resource_name = name.camelize.singularize
  db_schema_file = Rails.root.join('db/schema.rb')
  db_schema = DbSchema.new(File.exist?(db_schema_file) ? File.read(db_schema_file) : nil)
  yaml = YAML.load_file(Rails.root.join("config/infold/#{resource_name.underscore}.yml"))
  @resource = YamlReader.generate_resource(resource_name, yaml, db_schema)
end