Sha256: 317bf7585330ea6f19355fc1be6e0f17a90aacfa40aa5720a337a216cd91af17

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

class ActiveGraph::Generators::ModelGenerator < Rails::Generators::NamedBase #:nodoc:
  include ::ActiveGraph::Generators::SourcePathHelper
  include ::ActiveGraph::Generators::MigrationHelper

  argument :attributes, type: :array, default: [], banner: 'field:type field:type'

  check_class_collision

  class_option :timestamps, type: :boolean
  class_option :parent,     type: :string, desc: 'The parent class for the generated model'
  class_option :indices,    type: :array,  desc: 'The properties which should be indexed'
  class_option :has_one,    type: :array,  desc: 'A list of has_one relationships'
  class_option :has_many,   type: :array,  desc: 'A list of has_many relationships'

  def create_model_file
    template 'model.erb', File.join('app/models', class_path, "#{singular_name}.rb")
    migration_template 'migration.erb', 'create_'
  end

  protected

  def migration?
    false
  end

  def timestamps?
    options[:timestamps]
  end

  # rubocop:disable Naming/PredicateName
  def has_many?
    options[:has_many]
  end

  def has_many_statements
    options[:has_many].each_with_object('') do |key, txt|
      txt << has_x('has_many', key)
    end
  end

  def has_one?
    options[:has_one]
  end

  def has_x(method, key)
    to, from = key.split(':')
    (from ? "\n  #{method}(:#{to}).from(:#{from})\n" : "\n  #{method} :#{to}")
  end

  def has_one_statements
    txt = ''
    options[:has_one].each do |key|
      txt << has_x('has_one', key)
    end
    txt
  end
  # rubocop:enable Naming/PredicateName

  def indices?
    options[:indices]
  end

  def index_fragment(property)
    return if !options[:indices] || !options[:indices].include?(property)

    "index :#{property}"
  end

  def parent?
    options[:parent]
  end

  def timestamp_statements
    '
  property :created_at, type: DateTime
  # property :created_on, type: Date

  property :updated_at, type: DateTime
  # property :updated_on, type: Date

'
  end

  hook_for :test_framework
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activegraph-11.5.0.alpha.1 lib/active_graph/generators/model_generator.rb