From 74b684c6a7b49df53f7f389c637340056cb32612 Mon Sep 17 00:00:00 2001 From: Anthony Kirwan Date: Tue, 30 Sep 2014 11:12:19 -0500 Subject: [PATCH] generator to create representer with decorator pattern --- lib/generators/rails/representer_generator.rb | 5 ++++- lib/generators/rails/templates/decorator.rb | 6 ++++++ test/representer_generator_test.rb | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 lib/generators/rails/templates/decorator.rb diff --git a/lib/generators/rails/representer_generator.rb b/lib/generators/rails/representer_generator.rb index 21c3587..7e37024 100644 --- a/lib/generators/rails/representer_generator.rb +++ b/lib/generators/rails/representer_generator.rb @@ -9,8 +9,11 @@ class RepresenterGenerator < Rails::Generators::NamedBase class_option :format, :default => :json, :banner => "--format=JSON", :desc => "Use different formats JSON, JSON::HAL or XML" + class_option :decorator, type: :boolean, aliases: "-d", banner: "--decorator or -d", desc: "Use decorator instead of extend" + def generate_representer_file - template('representer.rb', file_path) + template = options[:decorator] ? 'decorator.rb' : 'representer.rb' + template(template, file_path) end private diff --git a/lib/generators/rails/templates/decorator.rb b/lib/generators/rails/templates/decorator.rb new file mode 100644 index 0000000..064b912 --- /dev/null +++ b/lib/generators/rails/templates/decorator.rb @@ -0,0 +1,6 @@ +class <%= class_name %>Representer < Roar::Decorator + include Roar::Representer::<%= format %> + <% property_options.each do |property| %> + <%= property -%> + <% end %> +end diff --git a/test/representer_generator_test.rb b/test/representer_generator_test.rb index ebd7d8a..b2fb181 100644 --- a/test/representer_generator_test.rb +++ b/test/representer_generator_test.rb @@ -14,6 +14,12 @@ class RepresentetGeneratorTest < Rails::Generators::TestCase assert_file representer_path('singer'), /module SingerRepresenter/ end + test "create a representer using using the decorator pattern" do + run_generator %w(singer -d) + + assert_file representer_path('singer'), /class SingerRepresenter < Roar::Decorator/ + end + test "create a representer with correct properties" do run_generator %w(singer name id)