Class: LeolayGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- LeolayGenerator
- Includes:
- ActiveLeonardo::Base
- Defined in:
- lib/generators/leolay/leolay_generator.rb
Instance Method Summary collapse
- #create_users_for_development ⇒ Object
-
#generate_layout ⇒ Object
def generate_layout template “config.rb”, “config/initializers/config.rb”.
- #setup_activeadmin ⇒ Object
- #setup_application ⇒ Object
- #setup_authentication ⇒ Object
- #setup_authorization ⇒ Object
- #setup_extras ⇒ Object
-
#setup_javascript ⇒ Object
def setup_formtastic return unless options.formtastic?.
-
#setup_rspec ⇒ Object
def setup_stylesheets app_path = “app/assets/stylesheets” vendor_path = “vendor/assets/stylesheets”.
Instance Method Details
#create_users_for_development ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/generators/leolay/leolay_generator.rb', line 220 def create_users_for_development return unless .authentication? file = "db/seeds.rb" append_file file do <<-FILE.gsub(/^ /, '') #{auth_class}.delete_all if #{auth_class}.count == 1 user=#{auth_class}.new :email => 'admin@#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234' #{"user.roles=['admin']" if .} user.save user=#{auth_class}.new :email => 'manager@#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234' #{"user.roles=['manager']" if .} user.save user=#{auth_class}.new :email => 'user@#{app_name}.com', :password => 'abcd1234', :password_confirmation => 'abcd1234' #{"user.roles=['user']" if .} user.save FILE end if File.exists?(file) end |
#generate_layout ⇒ Object
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 .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 .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 .jquery_ui?
if .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
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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 73 def generate_layout template "styles/#{style_name}/stylesheets/app/stylesheet.css.scss", "app/assets/stylesheets/#{style_name}.css.scss" template "styles/#{style_name}/stylesheets/app/custom_active_admin.css.scss", "app/assets/stylesheets/custom_active_admin.css.scss" template "styles/#{style_name}/stylesheets/app/_enviroment.css.scss", "app/assets/stylesheets/_enviroment.css.scss" copy_file "app/helpers/layout_helper.rb", "app/helpers/layout_helper.rb" directory "styles/#{style_name}/images", "app/assets/images/styles/#{style_name}" copy_file "styles/#{style_name}/favicon.ico", "app/assets/images/favicon.ico" copy_file "styles/#{style_name}/views/layout/application.html.erb", "app/views/layouts/application.html.erb", :force => true 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 .authentication? copy_file "config/initializers/config.rb", "config/initializers/config.rb" template "config/config.yml", "config/config.yml" 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 if .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 end end |
#setup_activeadmin ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/generators/leolay/leolay_generator.rb', line 368 def setup_activeadmin return unless .activeadmin? and activeadmin? template "config/initializers/activeadmin_leonardo.rb", "config/initializers/activeadmin_leonardo.rb" #copy_file "config/initializers/activeadmin_cancan.rb", "config/initializers/activeadmin_cancan.rb" if options.authorization? #template "app/admin/users.rb", "app/admin/#{options[:auth_class].downcase.pluralize}.rb" file = "app/admin/#{[:auth_class].downcase}.rb" inject_into_file file, :after => "ActiveAdmin.register #{[:auth_class]} do" do <<-FILE.gsub(/^ /, '') controller do def update unless params[:user]['password'] && params[:user]['password'].size > 0 params[:user].delete 'password' params[:user].delete 'password_confirmation' end super do #do something end end end FILE end file = "app/assets/stylesheets/active_admin.css.scss" append_file file do <<-FILE.gsub(/^ /, '') @import "custom_active_admin"; FILE end if File.exists?(file) file = "config/initializers/active_admin.rb" gsub_file file, 'config.fixture_path = "#{::Rails.root}/spec/fixtures"', '#config.fixture_path = "#{::Rails.root}/spec/fixtures"' #inject_into_file file, "#", :before => 'config.fixture_path = "#{::Rails.root}/spec/fixtures"' gsub_file file, "# config.authorization_adapter = ActiveAdmin::CanCanAdapter" do <<-FILE.gsub(/^ /, '') config.authorization_adapter = ActiveAdmin::CanCanAdapter FILE end if File.exists?(file) end |
#setup_application ⇒ Object
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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 107 def setup_application application do <<-FILE.gsub(/^ /, '') config.generators do |g| g.stylesheets false g.javascripts true g.leosca_controller :leosca_controller end config.autoload_paths += %W(\#{config.root}/lib/extras) I18n.enforce_available_locales = false FILE end file = "app/controllers/application_controller.rb" if File.exists?(file) inject_into_class file, ApplicationController do <<-FILE.gsub(/^ /, '') rescue_from CanCan::AccessDenied do |exception| redirect_to root_url, :alert => exception.message end def current_ability @current_ability ||= Ability.new(current_#{[:auth_class].downcase}) end FILE end if . inject_into_class file, ApplicationController do <<-FILE.gsub(/^ /, '') before_filter :localizate def localizate if params[:lang] session[:lang] = params[:lang] else session[:lang] ||= I18n.default_locale end I18n.locale = session[:lang] end FILE end end #file = "app/helpers/application_helper.rb" #if File.exists?(file) # inject_into_file file, :after => "module ApplicationHelper" do # <<-FILE.gsub(/^ /, '') # # def sortable(column, title = nil, remote = nil, path = nil, *params) # column = column.to_s # title ||= column.titleize # css_class = (column == sort_column) ? "sorted \#{sort_direction}" : nil # direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc" # params << "{:sort => column, :direction => direction}" # path = path ? eval("#\{path}(\#{params.join(',')})") : {:sort => column, :direction => direction} # link_to(title, path, :remote => true, :disable_with => t(:loading), :class => css_class) # end # FILE # end #end end |
#setup_authentication ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/generators/leolay/leolay_generator.rb', line 170 def setup_authentication file = "app/models/#{[:auth_class].downcase}.rb" #puts "File #{file} #{File.exists?(file) ? "" : "does not"} exists!" return unless .authentication? and File.exists?(file) inject_into_class file, auth_class do <<-FILE.gsub(/^ /, '') ROLES = %w[admin manager user guest] scope :with_role, lambda { |role| {:conditions => "roles_mask & \#{2**ROLES.index(role.to_s)} > 0 "} } def roles=(roles) self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum end def roles ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? } end def role_symbols roles.map(&:to_sym) end def role?(role) roles.include? role.to_s end def roles?(*roles) roles.each do |role| return true if role? role end nil end def admin? self.role? 'admin' end FILE end end |
#setup_authorization ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/generators/leolay/leolay_generator.rb', line 206 def file = "app/models/ability.rb" return unless File.exists?(file) gsub_file file, /initialize(\s|\()user(.|)/, "initialize(#{[:auth_class].downcase})" inject_into_file file, :before => " end\nend" do <<-FILE.gsub(/^ /, '') #{[:auth_class].downcase} ||= #{auth_class}.new can :manage, :all if #{[:auth_class].downcase}.role? :admin alias_action :batch_action, :to => :update can :read, ActiveAdmin::Page, :name => "Dashboard" FILE end end |
#setup_extras ⇒ Object
364 365 366 |
# File 'lib/generators/leolay/leolay_generator.rb', line 364 def setup_extras copy_file "lib/upd_array.rb", "lib/extras/upd_array.rb" end |
#setup_javascript ⇒ Object
def setup_formtastic
return unless .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
<<-FILE.gsub(/^ /, '')
Formtastic::FormBuilder.i18n_lookups_by_default = true
FILE
end if File.exists?(file)
path = "lib/templates/erb/"
remove_file path if File.exists?(path)
end
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/generators/leolay/leolay_generator.rb', line 264 def setup_javascript app_path = "app/assets/javascripts" file = "#{app_path}/custom.js.coffee" copy_file file, file file = "#{app_path}/active_admin.js.coffee" #gsub_file file, "#= require active_admin/base" do # <<-FILE.gsub(/^ /, '') # #= require jquery.turbolinks # # #= require active_admin/base #FILE #end append_file file do <<-FILE.gsub(/^ /, '') #= require turbolinks #= require custom FILE end end |
#setup_rspec ⇒ Object
def setup_stylesheets
app_path = "app/assets/stylesheets"
vendor_path = "vendor/assets/stylesheets"
file = "#{vendor_path}/vendor.css"
copy_file file, file
if .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
<<-FILE.gsub(/^ /, '')
*= require vendor
FILE
end
file = "#{vendor_path}/vendor.css"
inject_into_file file, :before => "*/" do
<<-FILE.gsub(/^ /, '')
*= require_tree ./jquery-ui
FILE
end
end
file = "#{vendor_path}/vendor.css"
inject_into_file file, :before => "*/" do
<<-FILE.gsub(/^ /, '')
*= require_tree ./formtastic
FILE
end if .formtastic?
end
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 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/generators/leolay/leolay_generator.rb', line 319 def setup_rspec file = "spec/spec_helper.rb" return unless File.exists?(file) && .rspec? inject_into_file file, :after => "require 'rspec/rails'" do <<-FILE.gsub(/^ /, '') require 'capybara/rspec' require 'helpers/application_helpers' Capybara.default_wait_time = 10 #default=2 FILE end gsub_file file, 'config.fixture_path = "#{::Rails.root}/spec/fixtures"', '#config.fixture_path = "#{::Rails.root}/spec/fixtures"' #inject_into_file file, "#", :before => 'config.fixture_path = "#{::Rails.root}/spec/fixtures"' gsub_file file, "config.use_transactional_fixtures = true" do <<-FILE.gsub(/^ /, '') config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end config.include ApplicationHelpers FILE end file = "spec/factories.rb" copy_file file, file file = "spec/support/devise.rb" copy_file file, file file = "spec/helpers/application_helpers.rb" copy_file file, file end |