Class: Angarium::Generators::PolicyGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/angarium/policy/policy_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_policyObject



13
14
15
# File 'lib/generators/angarium/policy/policy_generator.rb', line 13

def create_policy
  template "policy.rb", File.join("app/policies", "#{policy_name.underscore}.rb")
end

#enable_policyObject

Point config.policy_class at the generated class, uncommenting (or replacing) the line in the initializer so it takes effect immediately.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/angarium/policy/policy_generator.rb', line 19

def enable_policy
  initializer = "config/initializers/angarium.rb"

  unless File.exist?(File.join(destination_root, initializer))
    say_status :skip, %(#{initializer} not found; set config.policy_class = "#{class_name}" yourself), :yellow
    return
  end

  line = %r{^\s*#?\s*config\.policy_class\s*=.*$}
  if File.read(File.join(destination_root, initializer)).match?(line)
    gsub_file initializer, line, %(  config.policy_class = "#{class_name}")
  else
    say_status :skip, %(add config.policy_class = "#{class_name}" to #{initializer}), :yellow
  end
end