Class: Angularjs::InstallGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



63
64
65
# File 'lib/generators/angularjs/install/install_generator.rb', line 63

def app_name
  @app_name
end

#container_classObject (readonly)

Returns the value of attribute container_class.



63
64
65
# File 'lib/generators/angularjs/install/install_generator.rb', line 63

def container_class
  @container_class
end

#languageObject (readonly)

Returns the value of attribute language.



63
64
65
# File 'lib/generators/angularjs/install/install_generator.rb', line 63

def language
  @language
end

Instance Method Details

#generate_welcome_controllerObject



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
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/generators/angularjs/install/install_generator.rb', line 71

def generate_welcome_controller
  remove_file "public/index.html"
  uncomment_lines 'config/routes.rb', /root :to => 'welcome#index'/
    run "rails g controller welcome index"
  copy_file "AngularJS-medium.png", "app/assets/images/AngularJS-medium.png"
  copy_file 'favicon.ico', "app/assets/images/favicon.ico"
  empty_directory "app/assets/templates"
  empty_directory "app/assets/templates/welcome"
  copy_file "index_welcome.html.erb", "app/assets/templates/welcome/index.html.erb"
  if @language == 'coffeescript'
    if File.exists?('app/assets/javascripts/routes.js.erb')
      remove_file 'app/assets/javascripts/routes.js.erb' 
    end
    copy_file "routes.coffee.erb", "app/assets/javascripts/routes.coffee.erb"
    insert_into_file "app/assets/javascripts/routes.coffee.erb", @app_name, before: 'Client'
    ['csrf', 'welcome'].each do |prefix| 
      copy_file "#{prefix}_controller.js.coffee",
        "app/assets/javascripts/#{prefix}_controller.js.coffee"
      remove_file "app/assets/javascripts/#{prefix}_controller.js" 
    end
    # insert_into_file "app/assets/javascripts/welcome_controller.js.coffee", @app_name, before: 'Client'
  else # javascript
    if File.exists?('app/assets/javascripts/routes.coffee.erb')
      remove_file 'app/assets/javascripts/routes.coffee.erb' 
    end
    copy_file "routes.js.erb", "app/assets/javascripts/routes.js.erb" #if File.exists?("app/assets/javascripts/routes.js.erb")
    # Rails.logger.info "#{__FILE__}, #{__LINE__}, @app_name: #{@app_name}"
    insert_into_file "app/assets/javascripts/routes.js.erb", @app_name, before: 'Client'
    ['csrf', 'welcome'].each do |prefix| 
      copy_file "#{prefix}_controller.js",
        "app/assets/javascripts/#{prefix}_controller.js"
      remove_file "app/assets/javascripts/#{prefix}_controller.js.coffee" # if File.exists?("app/assets/javascripts/#{prefix}_controller.js.coffee")
    end
    # insert_into_file "app/assets/javascripts/welcome_controller.js", @app_name, before: 'Client'
  end
  # append_to_file "app/assets/javascripts/application.js",
  #   "//= require routes\n"
  # append_to_file "app/assets/javascripts/application.js",
  #   "//= require welcome_controller\n"
  append_to_file @application_css_file,
    ".center {text-align: center;}\n"
  insert_into_file "app/controllers/application_controller.rb",
%{
  private

  # AngularJS automatically sends CSRF token as a header called X-XSRF
  # this makes sure rails gets it
  def verified_request?
!protect_against_forgery? || request.get? ||
  form_authenticity_token == params[request_forgery_protection_token] ||
  form_authenticity_token == request.headers['X-XSRF-Token']
  end
 }, before: "end"

end

#init_angularjsObject



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/generators/angularjs/install/install_generator.rb', line 13

def init_angularjs
  if File.exist?('app/assets/javascripts/application.js')
    insert_into_file "app/assets/javascripts/application.js",
      "//= require_tree ./angularjs/\n", :after => "jquery_ujs\n"
  else
    copy_file "application.js", "app/assets/javascripts/application.js"
  end
  @application_css_file ='app/assets/stylesheets/application.css'
  if (!(File.exist?('app/assets/stylesheets/application.css')) &&
      File.exist?('app/assets/stylesheets/application.css.scss'))
    @application_css_file ='app/assets/stylesheets/application.css.scss'
  elsif !File.exist?('app/assets/stylesheets/application.css')
    create_file @application_css_file
  end
  directory "underscore", "app/assets/javascripts/underscore/"
  directory "angularjs", "app/assets/javascripts/angularjs/"
  if options["no-jquery"]
    gsub_file "app/assets/javascripts/application.js",
      /\/\/= require jquery_ujs\n/, ''
    gsub_file "app/assets/javascripts/application.js",
      /\/\/= require jquery\n/, ''
    #gsub_file "app/assets/javascripts/application.js",
    #  /^$\n/, ''
  end
end

#init_twitter_bootstrap_assetsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/angularjs/install/install_generator.rb', line 39

def init_twitter_bootstrap_assets
  unless options["no-bootstrap"]
    directory "fonts", "app/assets/fonts/"
    directory "fontawesome", "app/assets/stylesheets/fontawesome/"
    directory "bootstrap/css", "app/assets/stylesheets/bootstrap/"
    directory "bootstrap/js", "app/assets/javascripts/bootstrap/"
    directory "bootstrap/img", "app/assets/javascripts/img/"
    insert_into_file @application_css_file,
      " *= require bootstrap/bootstrap.min.css\n", :after => "require_self\n"
    insert_into_file @application_css_file,
      " *= require bootstrap/bootstrap-responsive.min.css\n",
      :after => "bootstrap.min.css\n"
    insert_into_file @application_css_file,
      " *= require fontawesome/font-awesome.css\n",
      :after => "bootstrap-responsive.min.css\n"
    append_to_file @application_css_file,
      "\nbody { padding-top: 60px; }\n"
    unless options["no-jquery"]
      insert_into_file "app/assets/javascripts/application.js",
        "//= require_tree ./bootstrap/\n", after: "angularjs/\n"
    end
  end
end

#init_twitter_bootstrap_layoutObject



64
65
66
67
68
69
# File 'lib/generators/angularjs/install/install_generator.rb', line 64

def init_twitter_bootstrap_layout
  @app_name = Rails.application.class.parent_name
  @container_class = options["layout-type"] == "fluid" ? "container-fluid" : "container"
  @language = options["language"] == 'javascript' ? "javascript" : "coffeescript"
  template "application.html.erb", "app/views/layouts/application.html.erb"
end