Class: Twofactor::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Twofactor::InstallGenerator
- Defined in:
- lib/generators/twofactor/install_generator.rb
Defined Under Namespace
Classes: InvalidParameterError, InvalidTemplateTypeError
Instance Method Summary collapse
-
#initialize(*runtime_args) ⇒ InstallGenerator
constructor
First arg : Model Name which needs Two-factor auth Second arg : Reference field that needs to be used(This field will appear in the client’s Google Authenticator app. Defaults to ‘email’) Third arg : Template type to use for twofactor_register default page (takes one of erb / haml / slim) Fourth arg : Controller that could be configured with TwoFactor actions( Defaults to Controller with modelname pluralized ) Fifth arg : Table name corresponding to the model.
- #install_twofactor ⇒ Object
Constructor Details
#initialize(*runtime_args) ⇒ InstallGenerator
First arg : Model Name which needs Two-factor auth Second arg : Reference field that needs to be used(This field will appear in the client’s Google Authenticator app. Defaults to ‘email’) Third arg : Template type to use for twofactor_register default page (takes one of erb / haml / slim) Fourth arg : Controller that could be configured with TwoFactor actions( Defaults to Controller with modelname pluralized ) Fifth arg : Table name corresponding to the model. ( Defaults to Rails’ choice. )
10 11 12 13 |
# File 'lib/generators/twofactor/install_generator.rb', line 10 def initialize(*runtime_args) super(*runtime_args) @args = runtime_args end |
Instance Method Details
#install_twofactor ⇒ Object
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 |
# File 'lib/generators/twofactor/install_generator.rb', line 25 def install_twofactor args = @args.first = {} [:model_name] = args.first [:reference_field] = args.second [:template_type] = args.third [:controller] = args.fourth [:table_name] = args.fifth model = [:model_name] @reference_field = [:reference_field] if(model.nil? || model.blank? || @reference_field.nil? || @reference_field.blank?) raise InvalidParameterError else @model_name = model.capitalize end raise InvalidTemplateTypeError unless ['haml','erb','slim'].include? [:template_type] controller_name = [:controller].present? ? [:controller] : "#{@model_name.pluralize}Controller" table_name = [:table_name].present? ? [:table_name] : @model_name.tableize template 'templates/twofactor_config.rb', 'config/initializers/twofactor_config.rb' template 'templates/twofactor_migration.rb', File.join('db', 'migrate', "#{Time.now.strftime('%Y%m%d%H%M%S')}_add_twofactor_fields_to_#{table_name}.rb") template_location = controller_name == 'ApplicationController' ? 'twofactor' : controller_name.gsub('Controller','').tableize copy_file "templates/twofactor_register.html.#{[:template_type]}", "app/views/#{template_location}/twofactor_register.html.#{[:template_type]}" twofactor_actions = "\n public\n"\ " def twofactor_register\n"\ " model_object = (Rails.application.class::TWOFACTOR_MODEL_NAME).constantize.find_by_id(params[:id])\n"\ " qrcode = Twofactor::TwoStep.enable_twofactor_auth(model_object)\n"\ " render :template => \"#{template_location}/twofactor_register\", :locals => {:qrcode => qrcode}\n"\ " end\n\n" inject_into_file "app/controllers/#{controller_name.underscore}.rb", twofactor_actions, :before => /^end/ c_name = controller_name.gsub("Controller","").underscore route "get '#{c_name}/twofactor_register/:id' => '#{c_name}#twofactor_register', as: 'twofactor_register'" end |