Class: Angularjs::ScaffoldGenerator

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

Instance Method Summary collapse

Instance Method Details

#columnsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/angularjs/scaffold/scaffold_generator.rb', line 25

def columns
  begin
    excluded_column_names = %w[id _id _type created_at updated_at]
    @model_name.constantize.columns.
      reject{|c| excluded_column_names.include?(c.name) }.
      collect{|c| ::Rails::Generators::GeneratedAttribute.
              new(c.name, c.type)}
  rescue NoMethodError
    @model_name.constantize.fields.
      collect{|c| c[1]}.
      reject{|c| excluded_column_names.include?(c.name) }.
      collect{|c|
        ::Rails::Generators::GeneratedAttribute.
          new(c.name, c.type.to_s)}
  end
end

#generateObject



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/generators/angularjs/scaffold/scaffold_generator.rb', line 42

def generate
  remove_file "app/assets/stylesheets/scaffolds.css.scss"
  # append_to_file "app/assets/javascripts/application.js",
  #   "//= require #{@plural_model_name}_controller\n"
  # append_to_file "app/assets/javascripts/application.js",
  #   "//= require #{@plural_model_name}\n"
  if language_option == 'coffeescript'
    insert_into_file "app/assets/javascripts/routes.coffee.erb",
      ", \'#{@plural_model_name}\'", :after => "'ngCookies'"
    insert_into_file "app/assets/javascripts/routes.coffee.erb",
%{when("/#{@plural_model_name}", 
controller: #{@controller}IndexCtrl
templateUrl: '<%= asset_path(\"#{@plural_model_name}/index.html\") %>'
  ).when("/#{@plural_model_name}/new",
controller: #{@controller}CreateCtrl
templateUrl: '<%= asset_path(\"#{@plural_model_name}/new.html\") %>'
  ).when("/#{@plural_model_name}/:id",
controller: #{@controller}ShowCtrl
templateUrl: '<%= asset_path(\"#{@plural_model_name}/show.html\") %>'
  ).when("/#{@plural_model_name}/:id/edit",
controller: #{@controller}EditCtrl
templateUrl: '<%= asset_path(\"#{@plural_model_name}/edit.html\") %>'
  ).}, :before => 'otherwise'
  else
    insert_into_file "app/assets/javascripts/routes.js.erb",
      ", '#{@plural_model_name}'", :after => "'ngCookies'"
    insert_into_file "app/assets/javascripts/routes.js.erb",
%{\n    when('/#{@plural_model_name}', {controller:#{@controller}IndexCtrl,
  templateUrl:'<%= asset_path("#{@plural_model_name}/index.html") %>'}).
when('/#{@plural_model_name}/new', {controller:#{@controller}CreateCtrl,
  templateUrl:'<%= asset_path("#{@plural_model_name}/new.html") %>'}).
when('/#{@plural_model_name}/:id', {controller:#{@controller}ShowCtrl,
  templateUrl:'<%= asset_path("#{@plural_model_name}/show.html") %>'}).
when('/#{@plural_model_name}/:id/edit', {controller:#{@controller}EditCtrl,
  templateUrl:'<%= asset_path("#{@plural_model_name}/edit.html") %>'}).}, :before => 'otherwise'
  end
  
  inject_into_class "app/controllers/#{@plural_model_name}_controller.rb",
    "#{@controller}Controller".constantize, "respond_to :json\n"
  template "new.html.erb",
    "app/assets/templates/#{@plural_model_name}/new.html.erb"
  template "edit.html.erb",
    "app/assets/templates/#{@plural_model_name}/edit.html.erb"
  template "show.html.erb",
    "app/assets/templates/#{@plural_model_name}/show.html.erb"
  template "index.html.erb",
    "app/assets/templates/#{@plural_model_name}/index.html.erb"

  model_index_link = "\n<li><%= link_to \'#{@controller_name}\', #{@plural_model_name}_path  %></li>"
  
  # insert_into_file "app/views/layouts/application.html.erb",  model_index_link,
  #   after: "<!-- sidebar menu models -->"

  insert_into_file "app/views/layouts/application.html.erb",  model_index_link,
    after: "<!-- main menu models -->"
  
  if language_option == 'coffeescript'
    remove_file "app/assets/javascripts/#{@plural_model_name}.js"
    remove_file "app/assets/javascripts/#{@plural_model_name}_controller.js"
    template "plural_model_name.js.coffee", "app/assets/javascripts/#{@plural_model_name}.js.coffee"
    template "plural_model_name_controller.js.coffee",
      "app/assets/javascripts/#{@plural_model_name}_controller.js.coffee"
  else
    remove_file "app/assets/javascripts/#{@plural_model_name}.js.coffee"
    remove_file "app/assets/javascripts/#{@plural_model_name}_controller.js.coffee"
    template "plural_model_name.js", "app/assets/javascripts/#{@plural_model_name}.js"
    template "plural_model_name_controller.js",
      "app/assets/javascripts/#{@plural_model_name}_controller.js"
    # remove the default .js.coffee file added by rails.
    remove_file "app/assets/javascripts/#{@plural_model_name}.js.coffee"
  end
end

#init_varsObject



18
19
20
21
22
23
# File 'lib/generators/angularjs/scaffold/scaffold_generator.rb', line 18

def init_vars
  @model_name = controller_name.singularize #"Post"
  @controller = controller_name #"Posts"
  @resource_name = @model_name.demodulize.underscore #post
  @plural_model_name = @resource_name.pluralize #posts
end

#language_optionObject



9
10
11
12
13
14
15
16
# File 'lib/generators/angularjs/scaffold/scaffold_generator.rb', line 9

def language_option
  if File.exist?("app/assets/javascripts/routes.js.erb")
    answer = 'javascript'
  else
    answer = 'coffeescript'
  end
  answer
end