mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Pulling larger example classes out into spec/samples
This commit is contained in:
parent
235dfd5933
commit
6a6f7e68da
7 changed files with 65 additions and 39 deletions
|
@ -3,7 +3,7 @@ module Draper
|
||||||
include ActionView::Helpers::TagHelper
|
include ActionView::Helpers::TagHelper
|
||||||
include ActionView::Helpers::UrlHelper
|
include ActionView::Helpers::UrlHelper
|
||||||
include ActionView::Helpers::TextHelper
|
include ActionView::Helpers::TextHelper
|
||||||
include ApplicationHelper if defined?(ApplicationHelper)
|
include ApplicationHelper #if defined?(ApplicationHelper)
|
||||||
|
|
||||||
require 'active_support/core_ext/class/attribute'
|
require 'active_support/core_ext/class/attribute'
|
||||||
class_attribute :denied, :allowed
|
class_attribute :denied, :allowed
|
||||||
|
@ -11,6 +11,11 @@ module Draper
|
||||||
|
|
||||||
DEFAULT_DENIED = Object.new.methods << :method_missing
|
DEFAULT_DENIED = Object.new.methods << :method_missing
|
||||||
self.denied = DEFAULT_DENIED
|
self.denied = DEFAULT_DENIED
|
||||||
|
|
||||||
|
def initialize(subject)
|
||||||
|
self.source = subject
|
||||||
|
build_methods
|
||||||
|
end
|
||||||
|
|
||||||
def self.denies(*input_denied)
|
def self.denies(*input_denied)
|
||||||
raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using denies" if input_denied.empty?
|
raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using denies" if input_denied.empty?
|
||||||
|
@ -20,15 +25,14 @@ module Draper
|
||||||
|
|
||||||
def self.allows(*input_allows)
|
def self.allows(*input_allows)
|
||||||
raise ArgumentError, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
|
raise ArgumentError, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
|
||||||
#raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denies == DEFAULT_EXCLUSIONS)
|
raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denied == DEFAULT_DENIED)
|
||||||
self.allowed = input_allows
|
self.allowed = input_allows
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(subject)
|
def self.decorate(input)
|
||||||
self.source = subject
|
input.respond_to?(:each) ? input.map{|i| new(i)} : new(input)
|
||||||
build_methods
|
end
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def select_methods
|
def select_methods
|
||||||
self.allowed || (source.public_methods - denied)
|
self.allowed || (source.public_methods - denied)
|
||||||
|
|
|
@ -13,14 +13,19 @@ describe Draper::Base do
|
||||||
subject.gsub("Sample"){|match| "Super"}.should == "Super String"
|
subject.gsub("Sample"){|match| "Super"}.should == "Super String"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a collection of wrapped objects when given a collection of source objects" do
|
context ".draper" do
|
||||||
pending("need to fix the proxying of blocks")
|
it "should return a collection of wrapped objects when given a collection of source objects" do
|
||||||
sources = ["one", "two", "three"]
|
sources = ["one", "two", "three"]
|
||||||
output = Draper::Base.new(sources)
|
output = Draper::Base.decorate(sources)
|
||||||
output.should respond_to(:each)
|
output.should respond_to(:each)
|
||||||
output.size.should == sources.size
|
output.size.should == sources.size
|
||||||
output.each{ |decorated| decorated.should be_instance_of(Draper::Base) }
|
output.each{ |decorated| decorated.should be_instance_of(Draper::Base) }
|
||||||
debugger
|
end
|
||||||
|
|
||||||
|
it "should return a single wrapped object when given a single source object" do
|
||||||
|
output = Draper::Base.decorate(source)
|
||||||
|
output.should be_instance_of(Draper::Base)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "echos the methods of the wrapped class" do
|
it "echos the methods of the wrapped class" do
|
||||||
|
@ -37,21 +42,6 @@ describe Draper::Base do
|
||||||
|
|
||||||
describe "a sample usage with denies" do
|
describe "a sample usage with denies" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
class DecoratorWithDenies < Draper::Base
|
|
||||||
denies :upcase
|
|
||||||
|
|
||||||
def sample_content
|
|
||||||
content_tag :span, "Hello, World!"
|
|
||||||
end
|
|
||||||
|
|
||||||
def sample_link
|
|
||||||
link_to "Hello", "/World"
|
|
||||||
end
|
|
||||||
|
|
||||||
def sample_truncate
|
|
||||||
ActionView::Helpers::TextHelper.truncate("Once upon a time", :length => 7)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:subject_with_denies){ DecoratorWithDenies.new(source) }
|
let(:subject_with_denies){ DecoratorWithDenies.new(source) }
|
||||||
|
@ -83,12 +73,6 @@ describe Draper::Base do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "a sample usage with allows" do
|
describe "a sample usage with allows" do
|
||||||
before(:all) do
|
|
||||||
class DecoratorWithAllows < Draper::Base
|
|
||||||
allows :upcase
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:subject_with_allows){ DecoratorWithAllows.new(source) }
|
let(:subject_with_allows){ DecoratorWithAllows.new(source) }
|
||||||
|
|
||||||
it "should echo the allowed method" do
|
it "should echo the allowed method" do
|
||||||
|
@ -114,14 +98,14 @@ describe Draper::Base do
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:using_allows_then_denies){
|
let(:using_allows_then_denies){
|
||||||
class DecoratorWithInvalidMixing < Draper::Base
|
class DecoratorWithAllowsAndDenies < Draper::Base
|
||||||
allows :upcase
|
allows :upcase
|
||||||
denies :downcase
|
denies :downcase
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:using_denies_then_allows){
|
let(:using_denies_then_allows){
|
||||||
class DecoratorWithInvalidMixing < Draper::Base
|
class DecoratorWithDeniesAndAllows < Draper::Base
|
||||||
denies :downcase
|
denies :downcase
|
||||||
allows :upcase
|
allows :upcase
|
||||||
end
|
end
|
||||||
|
@ -143,4 +127,11 @@ describe Draper::Base do
|
||||||
expect {using_denies_then_allows}.should raise_error(ArgumentError)
|
expect {using_denies_then_allows}.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "in a Rails application" do
|
||||||
|
it "should include ApplicationHelper if one exists" do
|
||||||
|
decorator = DecoratorApplicationHelper.decorate(Object.new)
|
||||||
|
decorator.uses_hello == "Hello, World!"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
5
spec/samples/application_helper.rb
Normal file
5
spec/samples/application_helper.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module ApplicationHelper
|
||||||
|
def hello
|
||||||
|
"Hello, World!"
|
||||||
|
end
|
||||||
|
end
|
5
spec/samples/decorator_application_helper.rb
Normal file
5
spec/samples/decorator_application_helper.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class DecoratorApplicationHelper < Draper::Base
|
||||||
|
def uses_hello
|
||||||
|
self.hello
|
||||||
|
end
|
||||||
|
end
|
3
spec/samples/decorator_with_allows.rb
Normal file
3
spec/samples/decorator_with_allows.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class DecoratorWithAllows < Draper::Base
|
||||||
|
allows :upcase
|
||||||
|
end
|
15
spec/samples/decorator_with_denies.rb
Normal file
15
spec/samples/decorator_with_denies.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
class DecoratorWithDenies < Draper::Base
|
||||||
|
denies :upcase
|
||||||
|
|
||||||
|
def sample_content
|
||||||
|
content_tag :span, "Hello, World!"
|
||||||
|
end
|
||||||
|
|
||||||
|
def sample_link
|
||||||
|
link_to "Hello", "/World"
|
||||||
|
end
|
||||||
|
|
||||||
|
def sample_truncate
|
||||||
|
ActionView::Helpers::TextHelper.truncate("Once upon a time", :length => 7)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,9 +1,12 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'bundler'
|
require 'bundler'
|
||||||
|
require './spec/samples/application_helper.rb'
|
||||||
Bundler.require
|
Bundler.require
|
||||||
|
Dir.glob('./spec/samples/*') {|file| require file}
|
||||||
require 'active_support'
|
require 'active_support'
|
||||||
require 'action_view'
|
require 'action_view'
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
|
|
||||||
require 'draper'
|
require 'draper'
|
||||||
|
|
||||||
Dir["spec/support/**/*.rb"].each do |file|
|
Dir["spec/support/**/*.rb"].each do |file|
|
||||||
|
|
Loading…
Add table
Reference in a new issue