Class: Medea::Generators::ScaffoldGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/medea/scaffold/scaffold_generator.rb

Instance Method Summary collapse

Methods inherited from Base

banner, source_root

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/medea/scaffold/scaffold_generator.rb', line 22

def initialize(*args, &block)
  super

  print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/

  @controller_actions = []
  @model_attributes = []
  @list_properties = {}
  @skip_model = options.skip_model?
  @namespace_model = options.namespace_model?
  @invert_actions = options.invert?

  list_types = %w(has owns)
  args_for_c_m.each do |arg|
    if arg == '!'
      @invert_actions = true
    elsif arg.include?(':')
      list = arg.split(':')
      #if the argument is a "has" or a "owns", we treat it as a list property.
      #Otherwise, it's a regular model attribute
      if list_types.include? list[0]
        @list_properties[list[0]] ||= []
        @list_properties[list[0]] << list[1..2]
      else
        @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
      end
    else
      @controller_actions << arg
      @controller_actions << 'create' if arg == 'new'
      @controller_actions << 'update' if arg == 'edit'
    end
  end

  @controller_actions.uniq!
  @model_attributes.uniq!

  if @invert_actions || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end

  #we don't do anything differently if there are no attributes passed.
  if @model_attributes.empty?
    if model_exists?
      model_columns_for_attributes.each do |column|
        @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
      end
    else
      @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
    end
  end
end

Instance Method Details

#add_gemsObject



74
75
76
77
# File 'lib/generators/medea/scaffold/scaffold_generator.rb', line 74

def add_gems
  add_gem "medea"
  add_gem "mocha", :group => :test
end

#create_controllerObject



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

def create_controller
  unless options.skip_controller?
    template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"

    template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"

    controller_actions.each do |action|
      if %w[index show new edit].include?(action) # Actions with templates
        template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
      end
    end

    if form_partial?
      template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
    end

    namespaces = plural_name.split('/')
    resource = namespaces.pop
    route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
      "namespace(:#{namespace}){ #{acc} }"
    }

    if test_framework == :rspec
      template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
    else
      template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
    end
  end
end

#create_modelObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/generators/medea/scaffold/scaffold_generator.rb', line 79

def create_model
  template 'model.rb.erb', "app/models/#{model_path}.rb"
  if test_framework == :rspec
    template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
    template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
  else
    template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
    template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
  end
end