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
- #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 end.
Instance Method Details
#create_users_for_development ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/generators/leolay/leolay_generator.rb', line 154 def create_users_for_development return unless .authentication? file = "db/seeds.rb" # Remove devise default user gsub_file file, /User\.create!\(.+\)/, "" if File.exists?(file) # Add default users append_file file do <<-FILE.gsub(/^ /, '') 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
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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 20 def generate_layout template "styles/#{style_name}/stylesheets/app/stylesheet.scss", "app/assets/stylesheets/#{style_name}.scss" template "styles/#{style_name}/stylesheets/app/custom_active_admin.scss", "app/assets/stylesheets/custom_active_admin.scss" template "styles/#{style_name}/stylesheets/app/_enviroment.scss", "app/assets/stylesheets/_enviroment.scss" copy_file "app/helpers/layout_helper.rb", "app/helpers/layout_helper.rb", :force => !.verbose? directory "styles/#{style_name}/images", "app/assets/images/styles/#{style_name}", :force => !.verbose? copy_file "styles/#{style_name}/favicon.ico", "app/assets/images/favicon.ico", :force => !.verbose? 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", :force => !.verbose? copy_file "styles/#{style_name}/views/layout/_session.html.erb", "app/views/application/_session.html.erb", :force => !.verbose? if .authentication? copy_file "config/initializers/config.rb", "config/initializers/config.rb", :force => !.verbose? 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| if f.include?("devise") copy_file f, "#{locale_path}/#{File.basename(f)}", :force => !.verbose? if .authentication? else copy_file f, "#{locale_path}/#{File.basename(f)}", :force => !.verbose? end end break if files.any? end end |
#setup_activeadmin ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 258 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.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
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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 50 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 end |
#setup_authentication ⇒ Object
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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 96 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 def name return unless self.email # Get left email text match_data = self.email.match(/^([a-z]+)(\.|\-|\_|)([a-z]+|)/i) name = "\#{match_data[1].capitalize}" name << " \#{match_data[3].capitalize}" unless match_data[3].blank? name end FILE end end |
#setup_authorization ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/generators/leolay/leolay_generator.rb', line 140 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
254 255 256 |
# File 'lib/generators/leolay/leolay_generator.rb', line 254 def setup_extras copy_file "lib/upd_array.rb", "lib/extras/upd_array.rb", :force => !.verbose? end |
#setup_javascript ⇒ Object
def setup_formtastic
return unless .formtastic?
end
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 180 def setup_javascript app_path = "app/assets/javascripts" file = "#{app_path}/custom.js.coffee" copy_file file, file, :force => !.verbose? file = "#{app_path}/active_admin.js.coffee" gsub_file file, "#= require active_admin/base" do <<-FILE.gsub(/^ /, '') #= require jquery #= require turbolinks #= require jquery.turbolinks #= require active_admin/base #= require custom FILE end append_file file do <<-FILE.gsub(/^ /, '') #= require turbolinks #= require jquery.turbolinks #= require custom FILE end end |
#setup_rspec ⇒ Object
def setup_stylesheets end
209 210 211 212 213 214 215 216 217 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 |
# File 'lib/generators/leolay/leolay_generator.rb', line 209 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, :force => !.verbose? file = "spec/support/devise.rb" copy_file file, file, :force => !.verbose? file = "spec/helpers/application_helpers.rb" copy_file file, file, :force => !.verbose? end |