Class: Infold::ModelGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_association_model_fileObject



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

def create_association_model_file
  @resource.associations&.select(&:has_child?)&.each do |association|
    # association_modelが未定義の場合、skip: trueで作成する
    option = association.field_group.has_association_model? ? { force: true } : { skip: true }
    @writer = ModelWriter.new(association)
    template "model.rb", Rails.root.join("app/models/admin", "#{association.model_name(:snake)}.rb"), **option
  end
end

#create_model_fileObject



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

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

#setupObject



11
12
13
14
15
16
17
# File 'lib/generators/infold/model_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