diff --git a/lib/generators/cancan/ability/USAGE b/lib/generators/cancan/ability/USAGE index 9719e39..692d6e9 100644 --- a/lib/generators/cancan/ability/USAGE +++ b/lib/generators/cancan/ability/USAGE @@ -1,4 +1,5 @@ Description: The cancan:ability generator creates an Ability class in the models directory. You can move this file anywhere you want as long as it - is in the load path. + is in the load path. A test/spec file is also generated depending + on if a spec directory exists. diff --git a/lib/generators/cancan/ability/ability_generator.rb b/lib/generators/cancan/ability/ability_generator.rb index 6c7ad24..5bc03c6 100644 --- a/lib/generators/cancan/ability/ability_generator.rb +++ b/lib/generators/cancan/ability/ability_generator.rb @@ -1,10 +1,15 @@ module Cancan module Generators class AbilityGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path("../templates", __FILE__) def generate_ability copy_file "ability.rb", "app/models/ability.rb" + if File.exist?(destination_path("spec")) + copy_file "ability_spec.rb", "spec/models/ability_spec.rb" + else + copy_file "ability_test.rb", "test/unit/ability_test.rb" + end end end end diff --git a/lib/generators/cancan/ability/templates/ability_spec.rb b/lib/generators/cancan/ability/templates/ability_spec.rb new file mode 100644 index 0000000..c4579f4 --- /dev/null +++ b/lib/generators/cancan/ability/templates/ability_spec.rb @@ -0,0 +1,16 @@ +require "spec_helper" +require "cancan/matchers" + +describe Ability do + describe "as guest" do + before(:each) do + @ability = Ability.new(nil) + end + + it "can only create a user" do + # Define what a guest can and cannot do + # @ability.should be_able_to(:create, :users) + # @ability.should_not be_able_to(:update, :users) + end + end +end diff --git a/lib/generators/cancan/ability/templates/ability_test.rb b/lib/generators/cancan/ability/templates/ability_test.rb new file mode 100644 index 0000000..f0a558c --- /dev/null +++ b/lib/generators/cancan/ability/templates/ability_test.rb @@ -0,0 +1,10 @@ +require "test_helper" + +class AbilityTest < ActiveSupport::TestCase + def guest_can_only_create_user + ability = Ability.new(nil) + # Define what a guest can and cannot do + # assert ability.can?(:create, :users) + # assert ability.cannot?(:update, :users) + end +end