Class: ScaffoldPlus::Generators::AuthorityGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/scaffold_plus/authority/authority_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_authorizerObject



33
34
35
36
# File 'lib/generators/scaffold_plus/authority/authority_generator.rb', line 33

def add_authorizer
  return if options.authorizer.present?
  template "authorizer.rb", "app/authorizers/#{name}_authorizer.rb"
end

#update_controllerObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/scaffold_plus/authority/authority_generator.rb', line 38

def update_controller
  return unless options.controller?
  file = "app/controllers/#{table_name}_controller.rb"
  inject_into_file file, after: /before_action :set_#{name}.*$/ do
    "\n  authorize_actions_for #{class_name}, only: [:index, :new, :create]"
  end
  inject_into_file file, after: /private$/ do
    "\n    def authority_forbidden(error)" +
    "\n      Authority.logger.warn(error.message)" +
    "\n      redirect_to root_path, alert: 'Forbidden'" +
    "\n    end\n"
  end
  inject_into_file file, after: /@#{name} = #{class_name}.find.*$/ do
    "\n      authorize_action_for(@#{name})"
  end
  inject_into_file file, after: /@#{name} = #{class_name}.friendly.find.*$/ do
    "\n      authorize_action_for(@#{name})"
  end
end

#update_modelObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/scaffold_plus/authority/authority_generator.rb', line 19

def update_model
  inject_into_class "app/models/#{name}.rb", class_name do
    text = options.before? ? "\n" : ""
    text << "  include Authority::Abilities\n"
    if options.authorizer.present?
      text << "  self.authorizer_name = '#{options.authorizer}'\n"
    else
      text << "  self.authorizer_name = '#{class_name}Authorizer'\n"
    end
    text << "\n" if options.after?
    text
  end
end