Class: Localtower::Generators::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/generators/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Model

Returns a new instance of Model.



4
5
6
# File 'lib/localtower/generators/model.rb', line 4

def initialize(opts)
  @opts = JSON[opts.to_json]
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/localtower/generators/model.rb', line 8

def run
  if not @opts['attributes'] or not @opts['model_name'].present?
    return nil
  end

  attributes_list = []

  @opts['attributes'].each do |attribute_data|
    str = "#{attribute_data["attribute_name"]}:#{attribute_data["attribute_type"]}"
    str << ":index" if attribute_data["index"]

    attributes_list << str
  end

  attributes_str = attributes_list.join(" ")
  cmd = "rails g model #{@opts['model_name'].camelize} #{attributes_str}"

  ::Localtower::Tools.perform_cmd(cmd, false)

  if defaults_present?
    insert_default_values.call
  end

  if @opts['run_migrate']
    ::Localtower::Tools.perform_cmd('rake db:migrate', false)
    # ::Localtower::Tools.perform_cmd('rake db:migrate RAILS_ENV=test', false)
  end

  self
end