From 933d9295ba80473faf0ee96e0afc8620c14098f5 Mon Sep 17 00:00:00 2001 From: John Otander Date: Tue, 22 Apr 2014 16:11:17 -0600 Subject: [PATCH 1/3] Add spec generator for policies. Add test hook. Remove old spec template. --- .../pundit/policy/policy_generator.rb | 2 ++ lib/generators/rspec/policy_generator.rb | 11 ++++++++ lib/generators/rspec/templates/policy_spec.rb | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 lib/generators/rspec/policy_generator.rb create mode 100644 lib/generators/rspec/templates/policy_spec.rb diff --git a/lib/generators/pundit/policy/policy_generator.rb b/lib/generators/pundit/policy/policy_generator.rb index 4b61320..474dbb6 100644 --- a/lib/generators/pundit/policy/policy_generator.rb +++ b/lib/generators/pundit/policy/policy_generator.rb @@ -6,6 +6,8 @@ module Pundit def create_policy template 'policy.rb', File.join('app/policies', class_path, "#{file_name}_policy.rb") end + + hook_for :test_framework end end end diff --git a/lib/generators/rspec/policy_generator.rb b/lib/generators/rspec/policy_generator.rb new file mode 100644 index 0000000..4a91a5f --- /dev/null +++ b/lib/generators/rspec/policy_generator.rb @@ -0,0 +1,11 @@ +module Rspec + module Generators + class PolicyGenerator < ::Rails::Generators::NamedBase + source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates')) + + def create_policy_spec + template 'policy_spec.rb', File.join('spec/policies', class_path, "#{file_name}_policy_spec.rb") + end + end + end +end diff --git a/lib/generators/rspec/templates/policy_spec.rb b/lib/generators/rspec/templates/policy_spec.rb new file mode 100644 index 0000000..80abde8 --- /dev/null +++ b/lib/generators/rspec/templates/policy_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe <%= class_name %>Policy do + + let(:user) { User.new } + + subject { <%= class_name %>Policy } + + permissions ".scope" do + pending "add some examples to (or delete) #{__FILE__}" + end + + permissions :create? do + pending "add some examples to (or delete) #{__FILE__}" + end + + permissions :show? do + pending "add some examples to (or delete) #{__FILE__}" + end + + permissions :update? do + pending "add some examples to (or delete) #{__FILE__}" + end + + permissions :destroy? do + pending "add some examples to (or delete) #{__FILE__}" + end +end From a0be3e179f91d7ce09bc6a7b3cdedbfe8409bc2e Mon Sep 17 00:00:00 2001 From: John Otander Date: Tue, 22 Apr 2014 22:02:03 -0600 Subject: [PATCH 2/3] Add autoload require for jruby. --- lib/pundit.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pundit.rb b/lib/pundit.rb index baabef2..04a2a77 100644 --- a/lib/pundit.rb +++ b/lib/pundit.rb @@ -3,6 +3,7 @@ require "pundit/policy_finder" require "active_support/concern" require "active_support/core_ext/string/inflections" require "active_support/core_ext/object/blank" +require "active_support/dependencies/autoload" module Pundit class NotAuthorizedError < StandardError From ab536852e01ca320348a5bdc4059b3ec76828372 Mon Sep 17 00:00:00 2001 From: John Otander Date: Wed, 23 Apr 2014 14:03:40 -0600 Subject: [PATCH 3/3] Add generator support for test_unit/minitest. --- lib/generators/test_unit/policy_generator.rb | 11 +++++++++++ .../test_unit/templates/policy_test.rb | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 lib/generators/test_unit/policy_generator.rb create mode 100644 lib/generators/test_unit/templates/policy_test.rb diff --git a/lib/generators/test_unit/policy_generator.rb b/lib/generators/test_unit/policy_generator.rb new file mode 100644 index 0000000..5baf50c --- /dev/null +++ b/lib/generators/test_unit/policy_generator.rb @@ -0,0 +1,11 @@ +module TestUnit + module Generators + class PolicyGenerator < ::Rails::Generators::NamedBase + source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates')) + + def create_policy_test + template 'policy_test.rb', File.join('test/policies', class_path, "#{file_name}_policy_test.rb") + end + end + end +end diff --git a/lib/generators/test_unit/templates/policy_test.rb b/lib/generators/test_unit/templates/policy_test.rb new file mode 100644 index 0000000..281737d --- /dev/null +++ b/lib/generators/test_unit/templates/policy_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class <%= class_name %>PolicyTest < ActiveSupport::TestCase + + def test_scope + end + + def test_create + end + + def test_show + end + + def test_update + end + + def test_destroy + end +end