Skip to content

Commit

Permalink
Merge pull request #416 from haines/inherit_security
Browse files Browse the repository at this point in the history
Inherit method security and test against Rails 3.x
  • Loading branch information
steveklabnik committed Jan 13, 2013
2 parents bc27aab + 380bbfd commit 1865ed3
Show file tree
Hide file tree
Showing 34 changed files with 297 additions and 226 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ rvm:
- rbx-19mode
- jruby-19mode
- ruby-head
env:
- "RAILS_VERSION=3.2"
- "RAILS_VERSION=3.1"
- "RAILS_VERSION=3.0"
matrix:
allow_failures:
- rvm: ruby-head
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ platforms :jruby do
gem "minitest", ">= 3.0"
gem "activerecord-jdbcsqlite3-adapter", "~> 1.2.2.1"
end

case ENV["RAILS_VERSION"]
when "master"
gem "rails", github: "rails/rails"
when "3.2", nil
gem "rails", "~> 3.2.0"
when "3.1"
gem "rails", "~> 3.1.0"
when "3.0"
gem "rails", "~> 3.0.0"
end
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ desc "Run all specs"
task "spec" => "spec:all"

namespace "spec" do
task "all" => ["draper", "generators", "minitest-rails", "integration"]
task "all" => ["draper", "generators", "integration"]

def spec_task(name)
desc "Run #{name} specs"
Expand All @@ -27,7 +27,6 @@ namespace "spec" do

spec_task "draper"
spec_task "generators"
spec_task "minitest-rails"

desc "Run integration specs"
task "integration" => ["db:setup", "integration:all"]
Expand All @@ -51,6 +50,8 @@ namespace "spec" do
end

task "test" do
puts "Running rake in dummy app"
ENV["RAILS_ENV"] = "test"
run_in_dummy_app "rake"
end
end
Expand All @@ -60,7 +61,8 @@ namespace "db" do
desc "Set up databases for integration testing"
task "setup" do
run_in_dummy_app "rm -f db/*.sqlite3"
run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed db:test:prepare"
run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed"
run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed"
run_in_dummy_app "RAILS_ENV=test rake db:schema:load"
end
end
4 changes: 2 additions & 2 deletions draper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Gem::Specification.new do |s|
s.add_dependency 'request_store', '~> 1.0.3'

s.add_development_dependency 'ammeter'
s.add_development_dependency 'rake', '~> 0.9.2'
s.add_development_dependency 'rake', '>= 0.9.2'
s.add_development_dependency 'rspec', '~> 2.12'
s.add_development_dependency 'rspec-mocks', '>= 2.12.1'
s.add_development_dependency 'yard'
s.add_development_dependency 'rspec-rails', '~> 2.12'
s.add_development_dependency 'minitest-rails', '~> 0.2'
s.add_development_dependency 'capybara'
end
17 changes: 15 additions & 2 deletions lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,24 @@ def self.define_proxy(method)
end

def self.security
@security ||= Security.new
@security ||= Security.new(superclass_security)
end

def self.security?
@security || (superclass.respond_to?(:security?) && superclass.security?)
end

def self.superclass_security
return nil unless superclass.respond_to?(:security)
superclass.security
end

def allow?(method)
self.class.security.allow?(method)
self.class.allow?(method)
end

def self.allow?(method)
!security? || security.allow?(method)
end

def handle_multiple_decoration(options)
Expand Down
9 changes: 5 additions & 4 deletions lib/draper/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class Railtie < Rails::Railtie
config.after_initialize do |app|
app.config.paths.add 'app/decorators', eager_load: true

# Test Support
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
require 'draper/test/test_unit_integration'
unless Rails.env.production?
require 'draper/test_case'
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
end
end

initializer "draper.setup_action_controller" do |app|
Expand Down
16 changes: 14 additions & 2 deletions lib/draper/security.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module Draper
# @private
class Security
def initialize

def initialize(parent = nil)
@strategy = parent.strategy if parent
@methods = []
@parent = parent
end

def denies(*methods)
Expand Down Expand Up @@ -32,9 +35,18 @@ def allow?(method)
end
end

protected

attr_reader :strategy

def methods
return @methods unless parent
@methods + parent.methods
end

private

attr_reader :methods, :strategy
attr_reader :parent

def apply_strategy(new_strategy)
raise ArgumentError, "Use only one of 'allows', 'denies', or 'denies_all'." if strategy && strategy != new_strategy
Expand Down
22 changes: 22 additions & 0 deletions lib/draper/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rake/testtask'

test_task = if Rails.version.to_f < 3.2
require 'rails/test_unit/railtie'
Rake::TestTask
else
require 'rails/test_unit/sub_test_task'
Rails::SubTestTask
end

namespace :test do
test_task.new(:decorators => "test:prepare") do |t|
t.libs << "test"
t.pattern = "test/decorators/**/*_test.rb"
end
end

if Rake::Task.task_defined?('test:run')
Rake::Task['test:run'].enhance do
Rake::Task['test:decorators'].invoke
end
end
5 changes: 0 additions & 5 deletions lib/draper/tasks/tu.rake

This file was deleted.

33 changes: 4 additions & 29 deletions lib/draper/test/minitest_integration.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
require 'minitest/rails/active_support'

module Draper
module MiniTest

class DecoratorTestCase < ::MiniTest::Rails::ActiveSupport::TestCase
include Draper::ViewHelpers::ClassMethods
alias_method :helper, :helpers

register_spec_type(self) do |desc|
desc < Draper::Decorator if desc.is_a?(Class)
end
register_spec_type(/Decorator( ?Test)?\z/i, self)
end

class Railtie < Rails::Railtie
config.after_initialize do |app|
if defined?(Capybara)
require 'capybara/rspec/matchers'
DecoratorTestCase.send :include, Capybara::RSpecMatchers
end

if defined?(Devise)
require 'draper/test/devise_helper'
DecoratorTestCase.send :include, Draper::DeviseHelper
end
end
end
class Draper::TestCase
register_spec_type(self) do |desc|
desc < Draper::Decorator || desc < Draper::CollectionDecorator if desc.is_a?(Class)
end

register_spec_type(/Decorator( ?Test)?\z/i, self)
end
39 changes: 7 additions & 32 deletions lib/draper/test/rspec_integration.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
module Draper
module RSpec
module DecoratorExampleGroup
include Draper::TestCase::Behavior
extend ActiveSupport::Concern

module DecoratorExampleGroup
extend ActiveSupport::Concern
included { metadata[:type] = :decorator }

include Draper::ViewHelpers::ClassMethods
alias_method :helper, :helpers
end

::RSpec.configure do |config|
# Automatically tag specs in specs/decorators as type: :decorator
config.include DecoratorExampleGroup, :type => :decorator, :example_group => {
:file_path => %r{spec/decorators}
}
end

class Railtie < Rails::Railtie
config.after_initialize do |app|
::RSpec.configure do |rspec|
if defined?(Capybara)
require 'capybara/rspec/matchers'
rspec.include Capybara::RSpecMatchers, :type => :decorator
end

if defined?(Devise)
require 'draper/test/devise_helper'
rspec.include Draper::DeviseHelper, :type => :decorator
end
end
end
end
included { metadata[:type] = :decorator }
end

RSpec.configure do |config|
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
end
end

18 changes: 0 additions & 18 deletions lib/draper/test/test_unit_integration.rb

This file was deleted.

33 changes: 33 additions & 0 deletions lib/draper/test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Draper
begin
require 'minitest/rails'
rescue LoadError
end

active_support_test_case = begin
require 'minitest/rails/active_support' # minitest-rails < 0.5
::MiniTest::Rails::ActiveSupport::TestCase
rescue LoadError
require 'active_support/test_case'
::ActiveSupport::TestCase
end

class TestCase < active_support_test_case
module Behavior
if defined?(::Devise)
require 'draper/test/devise_helper'
include Draper::DeviseHelper
end

if defined?(::Capybara) && (defined?(::RSpec) || defined?(::MiniTest::Matchers))
require 'capybara/rspec/matchers'
include ::Capybara::RSpecMatchers
end

include Draper::ViewHelpers::ClassMethods
alias_method :helper, :helpers
end

include Behavior
end
end
2 changes: 1 addition & 1 deletion lib/generators/test_unit/templates/decorator_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'test_helper'

class <%= class_name %>DecoratorTest < ActiveSupport::TestCase
class <%= class_name %>DecoratorTest < Draper::TestCase
end
2 changes: 1 addition & 1 deletion spec/draper/collection_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
end

it "raises error on invalid options" do
expect { Draper::CollectionDecorator.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
expect { Draper::CollectionDecorator.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, /Unknown key/)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/decorated_association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

it "raises error on invalid options" do
expect { Draper::DecoratedAssociation.new(owner, :association, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
expect { Draper::DecoratedAssociation.new(owner, :association, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, /Unknown key/)
end
end
end
Expand Down
Loading

0 comments on commit 1865ed3

Please sign in to comment.