Class: Gempire::Generator

Inherits:
Thor
  • Object
show all
Defined in:
lib/gempire/cli.rb

Instance Method Summary collapse

Instance Method Details

#add_default_gemsetObject

This method will add the default gem set to the Gemfile



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gempire/cli.rb', line 190

def add_default_gemset
  gems = <<-gems
gem 'simple_form'
gem 'stripe'
gem 'ledermann-rails-settings'
gem 'factory_girl_rails'
gem 'faker'
gem 'bcrypt', '~> 3.1.7'
gems


  File.open('Gemfile', 'a') do |f|
    f.puts(gems)
  end
end

#generateObject



20
21
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gempire/cli.rb', line 20

def generate

  ##
  # Add default gemset
  add_default_gemset

  puts 'Default gemset added'

  ##
  # Return early if no options were specified
  return 'Please specify an option. Use gempire --help to see a list of options.' if options.empty?

  ##
  # This is the control structure for generating everything related to the
  # the user model
  if options[:with_users]
    ##
    # Executes the rails generator for the user model and passes in the default
    # attributes
    exec 'rails generate model User first_name last_name email password_digest'

    user_content = <<-ruby
  # Settings provided by the ledermann-rails-settings gem
  has_settings do |s|
s.key :time_zone, defaults: {
  zone: 'UTC'
}
  end
ruby

    ##
    # Add the has_secure_password method to the user model
    lines = File.readlines('app/models/user.rb')
    lines.insert(1, "\thas_secure_password")
    File.open('app/models/user.rb', 'w') do |f|
      lines.each do |l|
        f.puts l
      end
    end

    ##
    # Create the log in functionality. This will create the sessions_controller
    template_path = File.expand_path('../templates/sessions_controller.rb', File.dirname(__FILE__))
    File.open('app/controllers/sessions_controller.rb', 'w') do |dest|
      File.open(template_path) do |template|
        dest.puts(template.read)
      end
    end

    ##
    # Now write some convenience methods to the application_controller
    definition = <<-definition
  helper_method :current_user
  helper_method :signed_in?

  def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def signed_in?
current_user.present?
  end

  def sign_in(user)
session[:user_id] = user.id
  end

  def after_sign_in
redirect_to dashboard_path
  end

  def after_sign_out
redirect_to sign_in_path
  end
definition

    ##
    # Read out the existing application_controller
    lines = File.readlines('app/controllers/application_controller.rb')
    lines.insert(1, definition)

    ##
    # Write back the application_controller with the new after_sign_in_method
    File.open('app/controllers/application_controller.rb', 'w') do |f|
      lines.each {|l| f.puts(l)}
    end

    routes = <<-routes
  get '/sign-in' => 'sessions#new', as: 'sign_in'
  post '/sessions' => 'sessions#create'
  delete '/sessions' => 'sessions#delete'
routes

    ##
    # Write the sessions routes to the routes.rb file
    lines = File.readlines('app/config/routes.rb')
    lines.insert(1, routes)

    File.open('app/config/routes.rb') do |f|
      lines.each {|l| f.puts(l)}
    end

    ##
    # Make the views/sessions directory if it's not already there
    if File.directory?('app/views/sesions') === false
      Dir.mkdir('app/views/sessions')
    end

    ##
    # Create the login form
    if options[:with_bootstrap]
      template_path = File.expand_path('../templates/sign_in_form_bootstrap.html.erb', File.dirname(__FILE__))
      File.open('app/views/sessions/new.html.erb') do |dest|
        File.open(template_path) do |template|
          dest.write(template.read)
        end
      end
    else
      template_path = File.expand_path('../templates/sign_in_form.html.erb', File.dirname(__FILE__))
      File.open('app/views/sessions/new.html.erb') do |dest|
        File.open(template_path) do |template|
          dest.write(template.read)
        end
      end
    end
  end

  ##
  # Control structure for using bootstrap
  if options[:with_bootstrap]
    text = <<-bower
{
  "directory": "vendor/assets/bower_components"
}
bower

    File.open('.bowerrc', 'w+') do |dest|
      dest.write(text)
    end


    exec 'mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss'

    File.open('app/assets/stylesheets/application.scss', 'w+') do |dest|
      dest.puts("@import 'bootstrap/dist/css/bootstrap';")
    end

    ##
    # Find what line to insert the bower components
    start_at = 0
    lines = File.readlines('config/application.rb')
    lines.each_with_index do |line, index|
      if line.include?('Rails::Application')
        starts_at = index
        break
      end
    end
    lines.insert(index, "\t\tconfig.assets.paths << Rails.root.join('bower_components')")
    File.open('config/application.rb', 'w') do |dest|
      lines.each {|line| dest.puts(line)}
    end

    # exec 'bower install --save bootstrap'
  end
end