Class: Effective::Generators::AbilityGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/effective/ability_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_ability ⇒ Object



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
49
50
51
52
53
# File 'lib/generators/effective/ability_generator.rb', line 22

def create_ability
  unless File.exists?('app/models/ability.rb')
    say_status(:skipped, :ability, :yellow) and return
  end

  Effective::CodeWriter.new('app/models/ability.rb') do |w|
    if resource.namespaces.blank?
      if w.find { |line, depth| depth == 2 && line == ability }
        say_status :identical, ability, :blue
      else
        w.insert_after_last(ability) { |line, depth| depth == 2 && line.start_with?('can ') } ||
        w.insert_before_last(ability) { |line, depth| depth == 2 && line.start_with?('if') } ||
        w.insert_before_last(ability) { |line, depth| depth == 2 && line == 'end' }

        say_status :ability, ability
      end
    end

    resource.namespaces.each do |namespace|
      w.within("if user.is?(:#{namespace})") do
        if w.find { |line, depth| depth == 1 && line == ability }
          say_status :identical, ability, :blue
        else
          w.insert_after_last(ability) { |line, depth| depth == 1 && line.start_with?('can ') } ||
          w.insert_before_last(ability) { |line, depth| depth == 1 && line == 'end' }

          say_status "#{namespace}_ability", ability
        end
      end
    end
  end
end

#invoke_ability ⇒ Object



18
19
20
# File 'lib/generators/effective/ability_generator.rb', line 18

def invoke_ability
  say_status :invoke, :ability, :white
end