From 138e0349ab921b3409d038226f301f6f7d943125 Mon Sep 17 00:00:00 2001 From: Jacques Fuentes Date: Fri, 10 Feb 2012 07:53:19 -0500 Subject: [PATCH 1/5] Add example of decorates_association to Readme --- Readme.markdown | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Readme.markdown b/Readme.markdown index ac2c0b58..61f2cd73 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -38,7 +38,7 @@ In summary, you can now: * [Use your decorators with CanCan](https://github.com/jcasimir/draper/commit/ac1f3083989107d877e2b1c918c3a3e792db99e8) * [Use a more generalized `options` hash in decorator initialization](https://github.com/jcasimir/draper/commit/03910877d0461356da0968a87346592908f292a7) * [Get better performance by generating methods](https://github.com/jcasimir/draper/commit/ebe30511b79eac82276413ca7ae54a4a4d86d4dc) -* [Automatically decorate associated objects](https://github.com/jcasimir/draper/commit/1580baa287997ed4e356aae0ffeeb8fe9c326ced) +* [Automatically decorate associated objects](https://github.com/jcasimir/draper/commit/1580baa287997ed4e356aae0ffeeb8fe9c326ced) See Example near bottom of Readme Thanks to [steveklabnik](http://github.com/steveklabnik), [i0rek](http://github.com/i0rek), [laserlemon](http://github.com/laserlemon), [michaelfairley](http://github.com/michaelfairley), [dnagir](http://github.com/dnagir), [ubermajestix](http://github.com/ubermajestix), [tmaier](http://github.com/tmaier), [angelim](http://github.com/angelim), [duncanbeevers](http://github.com/duncanbeevers), Albert Peng & JR Boyens, [leocassarani](http://github.com/leocassarani), [Jeff Felchner](http://github.com/Felchner), [shingara](http://github.com/shingara), [momolog](http://github.com/momolog), and [ayamomiji](http://github.com/ayamomiji) for their contributions to this version! @@ -326,6 +326,32 @@ class ArticleDecorator < ApplicationDecorator end end ``` + +### Example of Decorated Associations + +Add a `decorates_association :association_name` to gain access to a decorated version of your target association. + +```ruby +class ArticleDecorator < ApplicationDecorator + decorates :article + decorates_association :author # belongs_to :author association +end + +class AuthorDecorator < ApplicationDecorator + decorates :author + + def fancy_name + "#{model.title}. #{model.first_name} #{model.middle_name[0]}. #{model.last_name}" + end +end +``` + +Now when you call the association it will use a decorator. + +```ruby +<%= @article.author.fancy_name %> +``` + ## Issues / Pending * Documentation From 53586b8a38582a0ead1d20f9cea7ff0610ced572 Mon Sep 17 00:00:00 2001 From: Morton Jonuschat Date: Mon, 13 Feb 2012 10:58:07 +0100 Subject: [PATCH 2/5] Allow multiple allows --- lib/draper/base.rb | 3 ++- spec/draper/base_spec.rb | 11 +++++++++++ spec/spec_helper.rb | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index b91a1e63..62aebfe1 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -105,7 +105,8 @@ def self.denies(*input_denied) 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, "Use either 'allows' or 'denies', but not both." unless (self.denied == DEFAULT_DENIED) - self.allowed = input_allows + self.allowed ||= [] + self.allowed += input_allows end # Initialize a new decorator instance by passing in diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index dd71d933..d7e4cb40 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -515,6 +515,8 @@ module Paginator; def page_number; "magic_value"; end; end describe "a sample usage with allows" do let(:subject_with_allows){ DecoratorWithAllows.new(source) } + let(:subject_with_multiple_allows){ DecoratorWithMultipleAllows.new(source) } + it "should echo the allowed method" do subject_with_allows.should respond_to(:goodnight_moon) end @@ -522,6 +524,15 @@ module Paginator; def page_number; "magic_value"; end; end it "should echo _only_ the allowed method" do subject_with_allows.should_not respond_to(:hello_world) end + + it "should echo the combined allowed methods" do + subject_with_multiple_allows.should respond_to(:goodnight_moon) + subject_with_multiple_allows.should respond_to(:hello_world) + end + + it "should echo _only_ the combined allowed methods" do + subject_with_multiple_allows.should_not respond_to(:title) + end end describe "invalid usages of allows and denies" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5d04ab0a..8ea84da2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require './spec/support/samples/application_helper.rb' require './spec/support/samples/decorator.rb' require './spec/support/samples/decorator_with_allows.rb' +require './spec/support/samples/decorator_with_multiple_allows.rb' require './spec/support/samples/decorator_with_application_helper.rb' require './spec/support/samples/decorator_with_denies.rb' require './spec/support/samples/namespaced_product.rb' From a4927a51b5fd00d631074e7817f9cabbdcf4eb9b Mon Sep 17 00:00:00 2001 From: Morton Jonuschat Date: Mon, 13 Feb 2012 16:06:46 +0100 Subject: [PATCH 3/5] Add forgotten sample file --- spec/support/samples/decorator_with_multiple_allows.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 spec/support/samples/decorator_with_multiple_allows.rb diff --git a/spec/support/samples/decorator_with_multiple_allows.rb b/spec/support/samples/decorator_with_multiple_allows.rb new file mode 100644 index 00000000..760bea44 --- /dev/null +++ b/spec/support/samples/decorator_with_multiple_allows.rb @@ -0,0 +1,4 @@ +class DecoratorWithMultipleAllows < Draper::Base + allows :goodnight_moon + allows :hello_world +end From 3ac642140f76310df3e7647b6a8f8d9ba019b8d9 Mon Sep 17 00:00:00 2001 From: Morton Jonuschat Date: Mon, 13 Feb 2012 16:10:47 +0100 Subject: [PATCH 4/5] Refactor the initialization of allowed methods --- lib/draper/base.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 62aebfe1..572bf3d1 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -5,6 +5,7 @@ class Base attr_accessor :model, :options DEFAULT_DENIED = Object.new.methods << :method_missing + DEFAULT_ALLOWED = [] FORCED_PROXY = [:to_param, :id] FORCED_PROXY.each do |method| define_method method do |*args, &block| @@ -12,6 +13,7 @@ class Base end end self.denied = DEFAULT_DENIED + self.allowed = DEFAULT_ALLOWED # Initialize a new decorator instance by passing in # an instance of the source class. Pass in an optional @@ -89,7 +91,7 @@ def self.decorates_associations(*association_symbols) # @param [Symbols*] methods to deny like `:find, :find_by_name` 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, "Use either 'allows' or 'denies', but not both." if self.allowed? + raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.allowed == DEFAULT_ALLOWED) self.denied += input_denied end @@ -105,7 +107,6 @@ def self.denies(*input_denied) 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, "Use either 'allows' or 'denies', but not both." unless (self.denied == DEFAULT_DENIED) - self.allowed ||= [] self.allowed += input_allows end @@ -236,7 +237,7 @@ def source private def allow?(method) - (!allowed? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method) + (allowed.empty? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method) end end end From 56ce55addb90d11bcecc8cb757f0722b22b1c741 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 13 Feb 2012 10:24:31 -0500 Subject: [PATCH 5/5] Zomg removing trailing whitespace. --- doc/css/full_list.css | 4 +- doc/css/style.css | 60 +++++++++---------- doc/js/app.js | 20 +++---- doc/js/full_list.js | 16 ++--- lib/draper/decorated_enumerable_proxy.rb | 2 +- .../draper/decorator/decorator_generator.rb | 2 +- .../draper/install/install_generator.rb | 12 ++-- lib/generators/rails/decorator_generator.rb | 6 +- performance/bechmark.rb | 10 ++-- performance/decorators.rb | 8 +-- performance/models.rb | 4 +- spec/draper/base_spec.rb | 8 +-- spec/draper/helper_support_spec.rb | 2 +- spec/draper/view_context_spec.rb | 8 +-- .../draper/install/install_generator_spec.rb | 10 ++-- .../support/samples/application_controller.rb | 12 ++-- spec/support/samples/decorator_with_allows.rb | 2 +- .../decorator_with_application_helper.rb | 10 ++-- spec/support/samples/decorator_with_denies.rb | 2 +- 19 files changed, 99 insertions(+), 99 deletions(-) diff --git a/doc/css/full_list.css b/doc/css/full_list.css index f95e4e68..656ba518 100644 --- a/doc/css/full_list.css +++ b/doc/css/full_list.css @@ -1,6 +1,6 @@ -body { +body { margin: 0; - font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; + font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; font-size: 13px; height: 101%; overflow-x: hidden; diff --git a/doc/css/style.css b/doc/css/style.css index 2c70d8fd..d5c27d05 100644 --- a/doc/css/style.css +++ b/doc/css/style.css @@ -1,6 +1,6 @@ -body { +body { padding: 0 20px; - font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; + font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; font-size: 13px; } body.frames { padding: 0 5px; } @@ -8,7 +8,7 @@ h1 { font-size: 25px; margin: 1em 0 0.5em; padding-top: 4px; border-top: 1px dot h1.noborder { border-top: 0px; margin-top: 0; padding-top: 4px; } h1.title { margin-bottom: 10px; } h1.alphaindex { margin-top: 0; font-size: 22px; } -h2 { +h2 { padding: 0; padding-bottom: 3px; border-bottom: 1px #aaa solid; @@ -34,9 +34,9 @@ h2 small { font-weight: normal; font-size: 0.7em; display: block; float: right; #filecontents dd, .docstring dd { padding: 5px 0px; margin-left: 18px; } #filecontents dd > p, .docstring dd > p { margin: 0px; } -.note { +.note { color: #222; - -moz-border-radius: 3px; -webkit-border-radius: 3px; + -moz-border-radius: 3px; -webkit-border-radius: 3px; background: #e3e4e3; border: 1px solid #d5d5d5; padding: 7px 10px; display: block; } @@ -47,9 +47,9 @@ h2 small { font-weight: normal; font-size: 0.7em; display: block; float: right; .note.title { text-transform: lowercase; padding: 1px 5px; font-size: 0.9em; font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; display: inline; } .summary_signature + .note.title { margin-left: 7px; } h1 .note.title { font-size: 0.5em; font-weight: normal; padding: 3px 5px; position: relative; top: -3px; text-transform: capitalize; } -.note.title.constructor { color: #fff; background: #6a98d6; border-color: #6689d6; } -.note.title.writeonly { color: #fff; background: #45a638; border-color: #2da31d; } -.note.title.readonly { color: #fff; background: #6a98d6; border-color: #6689d6; } +.note.title.constructor { color: #fff; background: #6a98d6; border-color: #6689d6; } +.note.title.writeonly { color: #fff; background: #45a638; border-color: #2da31d; } +.note.title.readonly { color: #fff; background: #6a98d6; border-color: #6689d6; } .note.title.private { background: #d5d5d5; border-color: #c5c5c5; } .discussion .note { margin-top: 6px; } .discussion .note:first-child { margin-top: 0; } @@ -123,9 +123,9 @@ dl.constants .discussion *:last-child { margin-bottom: 0; } .method_details { border-top: 1px dotted #aaa; margin-top: 15px; padding-top: 0; } .method_details.first { border: 0; } -p.signature { - font-size: 1.1em; font-weight: normal; font-family: Monaco, Consolas, Courier, monospace; - padding: 6px 10px; margin-top: 18px; +p.signature { + font-size: 1.1em; font-weight: normal; font-family: Monaco, Consolas, Courier, monospace; + padding: 6px 10px; margin-top: 18px; background: #e5e8ff; border: 1px solid #d8d8e5; -moz-border-radius: 3px; -webkit-border-radius: 3px; } p.signature tt { font-family: Monaco, Consolas, Courier, monospace; } @@ -145,7 +145,7 @@ p.signature .aliases .names { font-family: Monaco, Consolas, Courier, monospace; .tags .examples h4 { padding: 0; margin: 0; margin-left: 15px; font-weight: bold; font-size: 0.9em; } .tags .overload .overload_item { list-style: none; margin-bottom: 25px; } -.tags .overload .overload_item .signature { +.tags .overload .overload_item .signature { padding: 2px 8px; background: #e5e8ff; border: 1px solid #d8d8e5; -moz-border-radius: 3px; -webkit-border-radius: 3px; } @@ -169,19 +169,19 @@ ul.summary { font-size: 1em; line-height: 1.5em; } -ul.summary a:link, ul.summary a:visited { +ul.summary a:link, ul.summary a:visited { text-decoration: none; font-size: 1.1em; } ul.summary li { margin-bottom: 5px; } -.summary .summary_signature { +.summary .summary_signature { padding: 1px 10px; background: #eaeaff; border: 1px solid #dfdfe5; - -moz-border-radius: 3px; -webkit-border-radius: 3px; + -moz-border-radius: 3px; -webkit-border-radius: 3px; } .summary_signature:hover { background: #eeeeff; cursor: pointer; } ul.summary.compact li { display: inline-block; margin: 0px 5px 0px 0px; line-height: 2.6em;} ul.summary.compact .summary_signature { padding: 5px 7px; padding-right: 4px; } -#content .summary_signature:hover a:link, +#content .summary_signature:hover a:link, #content .summary_signature:hover a:visited { background: transparent; color: #48f; @@ -204,18 +204,18 @@ ul.fullTree li:last-child { padding-bottom: 0; } .showAll .inheritName { display: none; } #search { position: absolute; right: 14px; top: 0px; } -#search a:link, #search a:visited { +#search a:link, #search a:visited { display: block; float: left; margin-right: 4px; padding: 8px 10px; text-decoration: none; color: #05a; border: 1px solid #d8d8e5; - -moz-border-radius-bottomleft: 3px; -moz-border-radius-bottomright: 3px; + -moz-border-radius-bottomleft: 3px; -moz-border-radius-bottomright: 3px; -webkit-border-bottom-left-radius: 3px; -webkit-border-bottom-right-radius: 3px; background: #eaf0ff; -webkit-box-shadow: -1px 1px 3px #ddd; } #search a:hover { background: #f5faff; color: #06b; } -#search a.active { - background: #568; padding-bottom: 20px; color: #fff; border: 1px solid #457; +#search a.active { + background: #568; padding-bottom: 20px; color: #fff; border: 1px solid #457; -moz-border-radius-topleft: 5px; -moz-border-radius-topright: 5px; -webkit-border-top-left-radius: 5px; -webkit-border-top-right-radius: 5px; } @@ -249,8 +249,8 @@ li.r2 { background: #fafafa; } z-index: 9999; background: #fff; display: none; - position: absolute; - top: 36px; + position: absolute; + top: 36px; right: 18px; width: 500px; height: 80%; @@ -266,7 +266,7 @@ li.r2 { background: #fafafa; } #content ul.summary li.deprecated .summary_signature a:link, #content ul.summary li.deprecated .summary_signature a:visited { text-decoration: line-through; font-style: italic; } -#toc { +#toc { padding: 20px; padding-right: 30px; border: 1px solid #ddd; float: right; background: #fff; margin-left: 20px; margin-bottom: 20px; max-width: 300px; -webkit-box-shadow: -2px 2px 6px #bbb; @@ -291,7 +291,7 @@ li.r2 { background: #fafafa; } #filecontents pre.code, .docstring pre.code, .source_code pre { font-family: monospace; } #filecontents pre.code, .docstring pre.code { display: block; } .source_code .lines { padding-right: 12px; color: #555; text-align: right; } -#filecontents pre.code, .docstring pre.code, +#filecontents pre.code, .docstring pre.code, .tags .example { padding: 5px 12px; margin-top: 4px; border: 1px solid #eef; background: #f5f5ff; } pre.code { color: #000; } pre.code .info.file { color: #555; } @@ -300,21 +300,21 @@ pre.code .tstring_content, pre.code .heredoc_beg, pre.code .heredoc_end, pre.code .qwords_beg, pre.code .qwords_end, pre.code .tstring, pre.code .dstring { color: #036A07; } -pre.code .fid, pre.code .id.new, pre.code .id.to_s, -pre.code .id.to_sym, pre.code .id.to_f, +pre.code .fid, pre.code .id.new, pre.code .id.to_s, +pre.code .id.to_sym, pre.code .id.to_f, pre.code .dot + pre.code .id, pre.code .id.to_i pre.code .id.each { color: #0085FF; } pre.code .comment { color: #0066FF; } pre.code .const, pre.code .constant { color: #585CF6; } pre.code .symbol { color: #C5060B; } -pre.code .kw, +pre.code .kw, pre.code .label, -pre.code .id.require, +pre.code .id.require, pre.code .id.extend, pre.code .id.include { color: #0000FF; } pre.code .ivar { color: #318495; } -pre.code .gvar, -pre.code .id.backref, +pre.code .gvar, +pre.code .id.backref, pre.code .id.nth_ref { color: #6D79DE; } pre.code .regexp, .dregexp { color: #036A07; } pre.code a { border-bottom: 1px dotted #bbf; } diff --git a/doc/js/app.js b/doc/js/app.js index 97095cbf..e1ac57d4 100644 --- a/doc/js/app.js +++ b/doc/js/app.js @@ -44,7 +44,7 @@ function createFullTreeLinks() { function fixBoxInfoHeights() { $('dl.box dd.r1, dl.box dd.r2').each(function() { - $(this).prev().height($(this).height()); + $(this).prev().height($(this).height()); }); } @@ -113,7 +113,7 @@ function summaryToggle() { if (next.hasClass('compact')) { next.toggle(); next.next().toggle(); - } + } else if (next.hasClass('summary')) { var list = $('
    '); list.html(next.html()); @@ -159,13 +159,13 @@ function generateTOC() { if ($('#' + proposedId).length > 0) { proposedId += counter; counter++; } this.id = proposedId; } - if (thisTag > lastTag) { - for (i = 0; i < thisTag - lastTag; i++) { - var tmp = $('
      '); toc.append(tmp); toc = tmp; - } + if (thisTag > lastTag) { + for (i = 0; i < thisTag - lastTag; i++) { + var tmp = $('
        '); toc.append(tmp); toc = tmp; + } } - if (thisTag < lastTag) { - for (i = 0; i < lastTag - thisTag; i++) toc = toc.parent(); + if (thisTag < lastTag) { + for (i = 0; i < lastTag - thisTag; i++) toc = toc.parent(); } toc.append('
      1. ' + $(this).text() + '
      2. '); lastTag = thisTag; @@ -174,7 +174,7 @@ function generateTOC() { html = ''; $('#content').prepend(html); $('#toc').append(_toc); - $('#toc .hide_toc').toggle(function() { + $('#toc .hide_toc').toggle(function() { $('#toc .top').slideUp('fast'); $('#toc').toggleClass('hidden'); $('#toc .title small').toggle(); @@ -183,7 +183,7 @@ function generateTOC() { $('#toc').toggleClass('hidden'); $('#toc .title small').toggle(); }); - $('#toc .float_toc').toggle(function() { + $('#toc .float_toc').toggle(function() { $(this).text('float'); $('#toc').toggleClass('nofloat'); }, function() { diff --git a/doc/js/full_list.js b/doc/js/full_list.js index 9e03dd45..58c7fb7c 100644 --- a/doc/js/full_list.js +++ b/doc/js/full_list.js @@ -10,7 +10,7 @@ function fullListSearch() { var link = $(this).find('.object_link a'); searchCache.push({name:link.text(), node:$(this), link:link}); }); - + $('#search input').keyup(function() { searchString = this.value.toLowerCase(); if (searchString === "") { @@ -18,9 +18,9 @@ function fullListSearch() { inSearch = null; $('#full_list, #content').removeClass('insearch'); $('#full_list li').removeClass('found').each(function() { - + var link = $(this).find('.object_link a'); - link.text(link.text()); + link.text(link.text()); }); if (clicked) { clicked.parents('ul').each(function() { @@ -38,7 +38,7 @@ function fullListSearch() { searchItem(); } }); - + $('#search input').focus(); $('#full_list').after("
        "); } @@ -54,8 +54,8 @@ function searchItem() { item.node.css('padding-left', '10px').addClass('found'); item.node.removeClass(lastRowClass).addClass(lastRowClass == 'r1' ? 'r2' : 'r1'); lastRowClass = item.node.hasClass('r1') ? 'r1' : 'r2'; - item.link.html(item.name.replace(new RegExp("(" + - searchString.replace(/([\/.*+?|()\[\]{}\\])/g, "\\$1") + ")", "ig"), + item.link.html(item.name.replace(new RegExp("(" + + searchString.replace(/([\/.*+?|()\[\]{}\\])/g, "\\$1") + ")", "ig"), '$1')); } @@ -110,10 +110,10 @@ function linkList() { function collapse() { if (!$('#full_list').hasClass('class')) return; - $('#full_list.class a.toggle').click(function() { + $('#full_list.class a.toggle').click(function() { $(this).parent().toggleClass('collapsed').next().toggleClass('collapsed'); highlight(); - return false; + return false; }); $('#full_list.class ul').each(function() { $(this).addClass('collapsed').prev().addClass('collapsed'); diff --git a/lib/draper/decorated_enumerable_proxy.rb b/lib/draper/decorated_enumerable_proxy.rb index 34b8701e..c360fee9 100644 --- a/lib/draper/decorated_enumerable_proxy.rb +++ b/lib/draper/decorated_enumerable_proxy.rb @@ -21,7 +21,7 @@ def method_missing (method, *args, &block) def respond_to?(method) super || @wrapped_collection.respond_to?(method) end - + def kind_of?(klass) super || @wrapped_collection.kind_of?(klass) end diff --git a/lib/generators/draper/decorator/decorator_generator.rb b/lib/generators/draper/decorator/decorator_generator.rb index 363f0f21..41110cad 100644 --- a/lib/generators/draper/decorator/decorator_generator.rb +++ b/lib/generators/draper/decorator/decorator_generator.rb @@ -7,7 +7,7 @@ class DecoratorGenerator < Rails::Generators::NamedBase generates: "app/decorators/article_decorator" "spec/decorators/article_decorator_spec" DESC - + source_root File.expand_path('../templates', __FILE__) DECORATORS_ROOT = 'app/decorators/' diff --git a/lib/generators/draper/install/install_generator.rb b/lib/generators/draper/install/install_generator.rb index 02d3e2d4..11d4e109 100644 --- a/lib/generators/draper/install/install_generator.rb +++ b/lib/generators/draper/install/install_generator.rb @@ -1,20 +1,20 @@ module Draper class InstallGenerator < Rails::Generators::Base - + desc <<-DESC Description: Generate application and spec decorators in your application. DESC - + class_option "test-framework", :type => :string, :default => "rspec", :aliases => "-t", :desc => "Test framework to be invoked" - + source_root File.expand_path('../templates', __FILE__) def build_application_decorator empty_directory 'app/decorators' template 'application_decorator.rb', File.join('app/decorators', 'application_decorator.rb') end - + def build_decorator_tests case options["test-framework"] when "rspec" @@ -23,13 +23,13 @@ def build_decorator_tests build_application_decorator_test end end - + private def build_application_decorator_spec empty_directory 'spec/decorators' template 'application_decorator_spec.rb', File.join('spec/decorators', 'application_decorator_spec.rb') end - + def build_application_decorator_test empty_directory 'test/decorators/' template 'application_decorator_test.rb', File.join('test/decorators', 'application_decorator_test.rb') diff --git a/lib/generators/rails/decorator_generator.rb b/lib/generators/rails/decorator_generator.rb index a0c9fe41..d2b30616 100644 --- a/lib/generators/rails/decorator_generator.rb +++ b/lib/generators/rails/decorator_generator.rb @@ -1,14 +1,14 @@ require File.expand_path('../../draper/decorator/decorator_generator.rb', __FILE__) class Rails::DecoratorGenerator < Draper::DecoratorGenerator - + source_root File.expand_path('../../draper/decorator/templates', __FILE__) - + class_option :invoke_after_finished, :type => :string, :description => "Generator to invoke when finished" def build_model_and_application_decorators super if self.options[:invoke_after_finished] - Rails::Generators.invoke(self.options[:invoke_after_finished], [@name, @_initializer.first[1..-1]]) + Rails::Generators.invoke(self.options[:invoke_after_finished], [@name, @_initializer.first[1..-1]]) end end diff --git a/performance/bechmark.rb b/performance/bechmark.rb index 6e92493d..32c63e43 100644 --- a/performance/bechmark.rb +++ b/performance/bechmark.rb @@ -17,20 +17,20 @@ ProductDecorator.decorate(Product.new) end end - + bm.report("#hello_world ") do i.times do |n| ProductDecorator.decorate(Product.new).hello_world end end - + bm.report("#sample_class_method ") do i.times do |n| ProductDecorator.decorate(Product.new).class.sample_class_method end end end - + puts "\n[ Defining methods on method_missing first hit ]" [ 1_000, 10_000, 100_000 ].each do |i| puts "\n[ #{i} ]" @@ -39,13 +39,13 @@ FastProductDecorator.decorate(FastProduct.new) end end - + bm.report("#hello_world ") do i.times do |n| FastProductDecorator.decorate(FastProduct.new).hello_world end end - + bm.report("#sample_class_method ") do i.times do |n| FastProductDecorator.decorate(FastProduct.new).class.sample_class_method diff --git a/performance/decorators.rb b/performance/decorators.rb index a6d467aa..80df11b6 100644 --- a/performance/decorators.rb +++ b/performance/decorators.rb @@ -1,11 +1,11 @@ require "./performance/models" class ProductDecorator < Draper::Base decorates :product - + def awesome_title "Awesome Title" end - + # Original #method_missing def method_missing(method, *args, &block) if allow?(method) @@ -23,11 +23,11 @@ def method_missing(method, *args, &block) class FastProductDecorator < Draper::Base decorates :product - + def awesome_title "Awesome Title" end - + # Modified #method_missing def method_missing(method, *args, &block) if allow?(method) diff --git a/performance/models.rb b/performance/models.rb index 28adbb2a..f9d106fa 100644 --- a/performance/models.rb +++ b/performance/models.rb @@ -3,7 +3,7 @@ class Product < ActiveRecord::Base def self.sample_class_method "sample class method" end - + def hello_world "Hello, World" end @@ -13,7 +13,7 @@ class FastProduct < ActiveRecord::Base def self.sample_class_method "sample class method" end - + def hello_world "Hello, World" end diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index d7e4cb40..4ce228d5 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -53,7 +53,7 @@ class BusinessDecorator < Draper::Base BusinessDecorator.model_class.should == Business end.should_not raise_error end - + context("accepts ActiveRecord like :class_name option too") do it "accepts constants for :class" do expect do @@ -63,7 +63,7 @@ class CustomDecorator < Draper::Base CustomDecorator.model_class.should == Product end.should_not raise_error end - + it "accepts constants for :class_name" do expect do class CustomDecorator < Draper::Base @@ -72,7 +72,7 @@ class CustomDecorator < Draper::Base CustomDecorator.model_class.should == Product end.should_not raise_error end - + it "accepts strings for :class" do expect do class CustomDecorator < Draper::Base @@ -81,7 +81,7 @@ class CustomDecorator < Draper::Base CustomDecorator.model_class.should == Product end.should_not raise_error end - + it "accepts strings for :class_name" do expect do class CustomDecorator < Draper::Base diff --git a/spec/draper/helper_support_spec.rb b/spec/draper/helper_support_spec.rb index 94d0508e..ccc7c95f 100644 --- a/spec/draper/helper_support_spec.rb +++ b/spec/draper/helper_support_spec.rb @@ -8,7 +8,7 @@ output = ApplicationController.decorate(@product){|p| p.model.object_id } output.should == @product.object_id end - + it 'uses #capture so Rails only renders the content once' do ApplicationController.decorate(@product){|p| p.model.object_id } ApplicationController.capture_triggered.should be diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index db94073f..ccad7564 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -4,19 +4,19 @@ let (:app_controller) do ApplicationController end - + let (:app_controller_instance) do app_controller.new end - + it "implements #set_current_view_context" do app_controller_instance.should respond_to(:set_current_view_context) end - + it "calls #before_filter with #set_current_view_context" do app_controller.before_filters.should include(:set_current_view_context) end - + it "raises an exception if the view_context is fetched without being set" do Draper::ViewContext.current = nil expect {app_controller.current_view_context}.should raise_exception(Exception) diff --git a/spec/generators/draper/install/install_generator_spec.rb b/spec/generators/draper/install/install_generator_spec.rb index eaaa4ebf..3cd09a31 100644 --- a/spec/generators/draper/install/install_generator_spec.rb +++ b/spec/generators/draper/install/install_generator_spec.rb @@ -19,24 +19,24 @@ it { should contain "class ApplicationDecorator < Draper::Base" } end end - + describe 'spec/decorators/application_decorator_spec.rb' do subject { file('spec/decorators/application_decorator_spec.rb') } it { should exist } it { should contain "describe ApplicationDecorator do" } end end - + context "using test_unit" do before { run_generator ["", "-t=test_unit"] } - + it_should_behave_like "ApplicationDecoratorGenerator" - + describe 'spec/decorators/application_decorator_spec.rb' do subject { file('spec/decorators/application_decorator_spec.rb') } it { should_not exist } end - + describe 'spec/decorators/application_decorator_test.rb' do subject { file('test/decorators/application_decorator_test.rb') } it { should exist } diff --git a/spec/support/samples/application_controller.rb b/spec/support/samples/application_controller.rb index 92ad2838..bfd70ea2 100644 --- a/spec/support/samples/application_controller.rb +++ b/spec/support/samples/application_controller.rb @@ -19,25 +19,25 @@ class ApplicationController < ActionController::Base extend ActionView::Helpers extend ActionView::Helpers::TagHelper extend ActionView::Helpers::UrlHelper - extend ApplicationHelper - + extend ApplicationHelper + def view_context @view_context ||= ApplicationController end - + def view_context=(input) @view_context = input end - + def self.hello "Hello!" end - + def self.capture(&block) @@capture = true block.call end - + def self.capture_triggered @@capture ||= false end diff --git a/spec/support/samples/decorator_with_allows.rb b/spec/support/samples/decorator_with_allows.rb index eda9901d..b7558ab3 100644 --- a/spec/support/samples/decorator_with_allows.rb +++ b/spec/support/samples/decorator_with_allows.rb @@ -1,3 +1,3 @@ -class DecoratorWithAllows < Draper::Base +class DecoratorWithAllows < Draper::Base allows :goodnight_moon end \ No newline at end of file diff --git a/spec/support/samples/decorator_with_application_helper.rb b/spec/support/samples/decorator_with_application_helper.rb index edc8956d..6fa1b59b 100644 --- a/spec/support/samples/decorator_with_application_helper.rb +++ b/spec/support/samples/decorator_with_application_helper.rb @@ -1,20 +1,20 @@ -class DecoratorWithApplicationHelper < Draper::Base +class DecoratorWithApplicationHelper < Draper::Base def uses_hello_world h.hello_world end - + def sample_content h.content_tag :span, "Hello, World!" end - + def sample_link h.link_to "Hello", "/World" end - + def sample_truncate h.truncate("Once upon a time", :length => 7) end - + def length "overridden" end diff --git a/spec/support/samples/decorator_with_denies.rb b/spec/support/samples/decorator_with_denies.rb index aa831c7d..f411c04b 100644 --- a/spec/support/samples/decorator_with_denies.rb +++ b/spec/support/samples/decorator_with_denies.rb @@ -1,3 +1,3 @@ -class DecoratorWithDenies < Draper::Base +class DecoratorWithDenies < Draper::Base denies :goodnight_moon, :title end