Class: LeolayGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/leolay/leolay_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_users_for_developmentObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/generators/leolay/leolay_generator.rb', line 175

def create_users_for_development
  return unless options.authentication?
  file = "db/seeds.rb"
  append_file file do
    "    user=User.new :email => 'admin@\#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234'\n    \#{\"user.roles=['admin']\" if options.authorization?}\n    user.save\n    user=User.new :email => 'manager@\#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234'\n    \#{\"user.roles=['manager']\" if options.authorization?}\n    user.save\n    user=User.new :email => 'user@\#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234'\n    \#{\"user.roles=['user']\" if options.authorization?}\n    user.save\n    FILE\n  end if File.exists?(file)\nend\n".gsub(/^      /, '')

#generate_layoutObject



14
15
16
17
18
19
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
# File 'lib/generators/leolay/leolay_generator.rb', line 14

def generate_layout
  template "config.rb", "config/initializers/config.rb"

  template "styles/#{style_name}/stylesheets/app/stylesheet.sass", "app/assets/stylesheets/#{style_name}.sass"
  copy_file "styles/#{style_name}/stylesheets/app/states.sass", "app/assets/stylesheets/states.sass"

  copy_file "app/helpers/layout_helper.rb", "app/helpers/layout_helper.rb"

  copy_file "styles/#{style_name}/views/layout/application.html.erb", "app/views/layouts/application.html.erb", :force => true
  template "styles/#{style_name}/views/layout/_layout.html.erb", "app/views/layouts/_#{style_name}.html.erb"
  copy_file "styles/#{style_name}/views/layout/_message.html.erb", "app/views/application/_message.html.erb"
  copy_file "styles/#{style_name}/views/layout/_session.html.erb", "app/views/application/_session.html.erb" if options.authentication?

  locale_path = "config/locales"
  source_paths.each do |source_path|
    files = Dir["#{source_path}/#{locale_path}/??.yml"]
    files.each do |f|
      copy_file f, "#{locale_path}/#{File.basename(f)}"
    end
    break if files.any?
  end

  copy_file "lib/utility.rb", "lib/extras/utility.rb"

  if options.pagination?
    source_paths.each do |source_path|
      files = Dir["#{source_path}/#{locale_path}/kaminari.??.yml"]
      files.each do |f|
        copy_file f, "#{locale_path}/#{File.basename(f)}"
      end
      break if files.any?
    end

    directory "styles/#{style_name}/views/kaminari", "app/views/kaminari"

    directory "styles/#{style_name}/images/kaminari", "app/assets/images/styles/#{style_name}/kaminari"
  end

  directory "styles/#{style_name}/images/style", "app/assets/images/styles/#{style_name}"

  directory "styles/#{style_name}/images/jquery-ui", "app/assets/images/styles/#{style_name}/jquery-ui" if options.jquery_ui?

  if options.authentication?
    source_paths.each do |source_path|
      files = Dir["#{source_path}/#{locale_path}/devise.??.yml"]
      files.each do |f|
        copy_file f, "#{locale_path}/#{File.basename(f)}"
      end
      break if files.any?
    end
    directory "app/views/devise", "app/views/devise"
  end
end

#setup_applicationObject



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
# File 'lib/generators/leolay/leolay_generator.rb', line 68

def setup_application
  application do
    "      config.generators do |g|\n          g.stylesheets         false\n          g.javascripts         false\n          g.leosca_controller   :leosca_controller\n        end\n\n        config.autoload_paths += %W(\\\#{config.root}/lib/extras)\n    FILE\n  end\n\n  file = \"app/controllers/application_controller.rb\"\n  if File.exists?(file)\n    inject_into_class file, ApplicationController do\n      <<-FILE.gsub(/^      /, '')\n      rescue_from CanCan::AccessDenied do |exception|\n        redirect_to root_url, :alert => exception.message\n      end\n      FILE\n    end if options.authorization?\n\n    inject_into_class file, ApplicationController do\n      <<-FILE.gsub(/^      /, '')\n      before_filter :localizate\n\n      def localizate\n        if params[:lang]\n          session[:lang] = params[:lang]\n        else\n          session[:lang] ||= I18n.default_locale\n        end\n        I18n.locale = session[:lang]\n      end\n      def get_operator(str, default='=')\n        m = str.match(/^(\\<|\\>|\\=)(\\>|\\=|)/)\n        m ? m.to_s : default\n      end\n      def get_value(str)\n        str.sub(get_operator(str), '').strip\n      end\n      FILE\n    end\n  end\n\n  file = \"app/helpers/application_helper.rb\"\n  if File.exists?(file)\n    inject_into_file file, :after => \"module ApplicationHelper\" do\n      <<-FILE.gsub(/^      /, '')\n\n      def sortable(column, title = nil, remote = nil, path = nil, *params)\n        column = column.to_s\n        title ||= column.titleize\n        css_class = (column == sort_column) ? \"sorted \\\#{sort_direction}\" : nil\n        direction = (column == sort_column && sort_direction == \"asc\") ? \"desc\" : \"asc\"\n        params << \"{:sort => column, :direction => direction}\"\n        path = path ? eval(\"#\\{path}(\\\#{params.join(',')})\") : {:sort => column, :direction => direction}\n        link_to(title, path, :remote => true, :disable_with => t(:loading), :class => css_class)\n      end\n      FILE\n    end\n  end\nend\n".gsub(/^      /, '')

#setup_authenticationObject



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
# File 'lib/generators/leolay/leolay_generator.rb', line 133

def setup_authentication
  file = "app/models/#{options.auth_class}.rb"
  #puts "File #{file} #{File.exists?(file) ? "" : "does not"} exists!"
  return unless options.authentication? and File.exists?(file)

  inject_into_class file, User do
    "    ROLES = %w[admin manager user guest]\n    scope :with_role, lambda { |role| {:conditions => \"roles_mask & \\\#{2**ROLES.index(role.to_s)} > 0 \"} }\n\n    def roles=(roles)\n      self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum\n    end\n    def roles\n      ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }\n    end\n    def role_symbols\n      roles.map(&:to_sym)\n    end\n    def role?(role)\n      roles.include? role.to_s\n    end\n    def admin?\n      self.role? 'admin'\n    end\n    FILE\n  end\n\nend\n".gsub(/^    /, '')

#setup_authorizationObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/generators/leolay/leolay_generator.rb', line 163

def setup_authorization
  file = "app/models/ability.rb"
  return unless File.exists?(file)
  inject_into_file file, :before => "  end\nend" do
    "        user ||= User.new\n        can :manage, :all if user.role? :admin\n\n    FILE\n  end\nend\n".gsub(/^      /, '')

#setup_formtasticObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/generators/leolay/leolay_generator.rb', line 193

def setup_formtastic
  return unless options.formtastic?

  path = "styles/#{style_name}/stylesheets/vendor/formtastic"
  dest_path = "vendor/assets/stylesheets/formtastic"

  path = dest_path unless File.exists?(path)

  file = "#{path}/formtastic.css"
  copy_file file, file unless File.exists?(file)
  file = "#{path}/formtastic_changes.css"
  copy_file file, file

  file = "config/initializers/formtastic.rb"
  inject_into_file file, :after => "# Formtastic::FormBuilder.i18n_lookups_by_default = false" do
    "\n    Formtastic::FormBuilder.i18n_lookups_by_default = true\n    FILE\n  end if File.exists?(file)\n\n  path = \"lib/templates/erb/\"\n  remove_file path if File.exists?(path)\nend\n".gsub(/^      /, '')

#setup_javascriptObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/generators/leolay/leolay_generator.rb', line 218

def setup_javascript
  app_path = "app/assets/javascripts"
  vendor_path = "vendor/assets/javascripts"

  file = "#{app_path}/custom.js"
  copy_file file, file

  file = "#{vendor_path}/vendor.js"
  copy_file file, file

  file = "#{app_path}/application.js"
  append_file file do
    "\n    //= require vendor\n    FILE\n  end\n\n  if options.jquery_ui?\n    file = \"\#{app_path}/custom.js\"\n    #append_file file do\n    #  <<-FILE.gsub(/^        /, '')\n    #\n    #  $(function (){\n    #    $('.calendar').datepicker();\n    #  });\n    #  FILE\n    #end\n    inject_into_file file, :after => \"$(document).ready(function() {\" do\n      <<-FILE.gsub(/^    /, '')\n\n      $('.calendar').datepicker();\n      FILE\n    end\n\n    file = \"\#{app_path}/application.js\"\n    inject_into_file file, :after => \"//= require jquery_ujs\" do\n      <<-FILE.gsub(/^        /, '')\n\n      //= require jquery-ui\n      FILE\n    end\n\n    directory \"\#{vendor_path}/jquery-ui\", \"\#{vendor_path}/jquery-ui\"\n\n    file = \"\#{vendor_path}/vendor.js\"\n    append_file file do\n      <<-FILE.gsub(/^        /, '')\n\n      //= require_tree ./jquery-ui\n      FILE\n    end\n\n  end\nend\n".gsub(/^      /, '')

#setup_rspecObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/generators/leolay/leolay_generator.rb', line 307

def setup_rspec
  file = "spec/spec_helper.rb"
  return unless File.exists?(file) && options.rspec?
  inject_into_file file, :after => "require 'rspec/rails'" do
  "\n  require 'capybara/rspec'\n  require 'helpers/application_helpers_spec'\n\n  Capybara.default_wait_time = 10 #default=2\n  FILE\n  end\n\n  gsub_file file, 'config.fixture_path = \"\#{::Rails.root}/spec/fixtures\"', '#config.fixture_path =  \"\#{::Rails.root}/spec/fixtures\"'\n  #inject_into_file file, \"#\", :before => 'config.fixture_path = \"\#{::Rails.root}/spec/fixtures\"'\n\n  gsub_file file, \"config.use_transactional_fixtures = true\" do\n  <<-FILE.gsub(/^  /, '')\nconfig.use_transactional_fixtures = false\n\n  config.before(:suite) do\n    DatabaseCleaner.strategy = :truncation\n  end\n\n  config.before(:each) do\n    DatabaseCleaner.start\n  end\n\n  config.after(:each) do\n    DatabaseCleaner.clean\n  end\n\n  config.include ApplicationHelpers\n  FILE\n  end\n\n  file = \"spec/factories.rb\"\n  copy_file file, file\n  file = \"spec/support/devise.rb\"\n  copy_file file, file\n  file = \"spec/helpers/application_helpers_spec.rb\"\n  copy_file file, file\n\nend\n".gsub(/^    /, '')

#setup_stylesheetsObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/generators/leolay/leolay_generator.rb', line 274

def setup_stylesheets
  app_path = "app/assets/stylesheets"
  vendor_path = "vendor/assets/stylesheets"

  file = "#{vendor_path}/vendor.css"
  copy_file file, file

  if options.jquery_ui?
    directory "styles/#{style_name}/stylesheets/vendor/jquery-ui", "vendor/assets/stylesheets/jquery-ui"

    file = "#{app_path}/application.css"
    inject_into_file file, :before => "*/" do
      "       *= require vendor\n      FILE\n    end\n\n    file = \"\#{vendor_path}/vendor.css\"\n    inject_into_file file, :before => \"*/\" do\n      <<-FILE.gsub(/^        /, '')\n       *= require_tree ./jquery-ui\n      FILE\n    end\n  end\n\n  file = \"\#{vendor_path}/vendor.css\"\n  inject_into_file file, :before => \"*/\" do\n    <<-FILE.gsub(/^      /, '')\n     *= require_tree ./formtastic\n    FILE\n  end if options.formtastic?\nend\n".gsub(/^        /, '')