Class: Mdwa::Generators::ScaffoldGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/mdwa/scaffold/scaffold_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 29

def initialize(*args, &block)

  super

  @model = MDWA::Generators::Model.new( scaffold_name )        

  # model_name is not valid
  print_usage unless @model.valid?

  # verifies specific model name
  @specific_model = MDWA::Generators::Model.new( options.model ) unless options.model.blank?
  if !@specific_model.nil?
    if !@specific_model.valid?
      print_usage 
    else
      @model.specific_model_name = @specific_model.raw
    end
  end

  # sets the model attributes
  attributes.each do |attribute|
    @model.add_attribute MDWA::Generators::ModelAttribute.new( attribute )
  end

end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



14
15
16
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 14

def model
  @model
end

#specific_modelObject

Returns the value of attribute specific_model.



14
15
16
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 14

def specific_model
  @specific_model
end

Instance Method Details

#associationsObject



133
134
135
136
137
138
139
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 133

def associations
  unless options.only_interface
    @model.attributes.select{ |a| a.references? }.each do |attr|
        generate "mdwa:association #{@model.raw} #{attr.reference_type} #{attr.type.raw} #{'--skip_migrations' if options.skip_migrations} #{'--force' if options.force} #{'--ask' unless options.skip_questions} --skip_rake_migrate"
    end
  end
end

#controllerObject



55
56
57
58
59
60
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 55

def controller
  unless options.skip_interface
    @inherit_controller = 'A::BackendController' if @model.space == 'a'
    template "controllers/#{'ajax_' if options.ajax}controller.rb", "app/controllers/#{@model.space}/#{@model.plural_name}_controller.rb"
  end
end

#localesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 90

def locales
  
  locales_file = 'config/locales/mdwa_model_specific.en.yml'
  create_file locales_file unless File.exist?(locales_file)

  append_file locales_file, :after => "en:\n" do 
    lines = []
    lines <<  "  #{@model.plural_name}:"
    lines <<  "    create_success: \"#{@model.singular_name.humanize} created.\""
    lines <<  "    update_success: \"#{@model.singular_name.humanize} updated.\""
    lines <<  "    destroy_success: \"#{@model.singular_name.humanize} destroyed.\""
    lines <<  "    index_title: \"#{@model.plural_name.humanize}\""
    lines <<  "    show_title: \"#{@model.singular_name.humanize}\""
    lines <<  "    new_title: \"New #{@model.singular_name.humanize}\""
    lines <<  "    edit_title: \"Edit #{@model.singular_name.humanize}\""
    @model.attributes.each do |attr|
      lines <<  "    index_#{attr.name}: \"#{attr.name.humanize}\""
      lines <<  "    show_#{attr.name}: \"#{attr.name.humanize}\""
    end
    lines << "\n"
    lines.join("\n")
  end
end

#migrationObject



127
128
129
130
131
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 127

def migration
  unless options.skip_migrations or options.only_interface
    migration_template 'db_migrate/migrate.rb', "db/migrate/create_#{@model.plural_name}.rb"
  end
end

#routesObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 114

def routes
  unless options.skip_interface
    route_str = []
    route_str << "namespace :#{@model.space} do" if @model.namespace?
    route_str << "\t\tcontroller :#{@model.plural_name} do"
    route_str << "\t\tend"
    route_str << "\t\tresources :#{@model.plural_name}"
    route_str << "\tend" if @model.namespace?
    
    route route_str.join("\n")
  end
end

#run_rake_db_migrateObject



141
142
143
144
145
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 141

def run_rake_db_migrate
  if !options.skip_rake_migrate and !options.skip_migrations and !options.only_interface
    rake('db:migrate') if !options.skip_questions and yes? 'Run rake db:migrate?'
  end
end

#tests_and_specifyObject



147
148
149
150
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 147

def tests_and_specify
  template 'specs/model.rb', "spec/models/#{@model.space}/#{@model.singular_name}_spec.rb"
  template 'specs/routing.rb', "spec/routing/#{@model.space}/#{@model.plural_name}_routing_spec.rb"
end

#viewsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/generators/mdwa/scaffold/scaffold_generator.rb', line 70

def views
  unless options.skip_interface
    template 'views/edit.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/edit.html.erb"
    template 'views/index.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/index.html.erb"
    template 'views/index.js.erb', "app/views/#{@model.space}/#{@model.plural_name}/index.js.erb"
    template 'views/new.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/new.html.erb"
    template 'views/show.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/show.html.erb"
    template 'views/_form.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/_form.html.erb"
    template 'views/_form_fields.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/_form_fields.html.erb"
    template 'views/_list.html.erb', "app/views/#{@model.space}/#{@model.plural_name}/_#{@model.plural_name}.html.erb"

    if options.ajax
      #create and update are the same
      template 'views/create.js.erb', "app/views/#{@model.space}/#{@model.plural_name}/create.js.erb"
      template 'views/create.js.erb', "app/views/#{@model.space}/#{@model.plural_name}/update.js.erb"
      template 'views/destroy.js.erb', "app/views/#{@model.space}/#{@model.plural_name}/destroy.js.erb"
    end
  end
end