From b2cdc28c8e72031254e1cf3d1cf7c7f94e658861 Mon Sep 17 00:00:00 2001 From: JKHiggins Date: Wed, 19 Feb 2014 17:05:14 -0600 Subject: [PATCH 1/4] Added images to tandem resource generator -If you want to add an image to a resource you must append --with_image to the end of the normal generator line -An image can also be added AND deleted from the edit form for a resource TODO: -Still needs some nice styling but its now usable -Tests have yet to be written --- .../tandem/resource/resource_generator.rb | 31 +++++++++++++++++-- .../tandem/resource/templates/_form.html.slim | 13 +++++++- .../tandem/resource/templates/show.html.slim | 6 ++++ spec/dummy/db/schema.rb | 2 ++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/generators/tandem/resource/resource_generator.rb b/lib/generators/tandem/resource/resource_generator.rb index e3a5d1f..adabaad 100644 --- a/lib/generators/tandem/resource/resource_generator.rb +++ b/lib/generators/tandem/resource/resource_generator.rb @@ -5,15 +5,17 @@ module Tandem module Generators class ResourceGenerator < ::Rails::Generators::NamedBase include ::Rails::Generators::ResourceHelpers - - desc "Create a tandem resource" - argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" + desc "Create a tandem resource" + argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" + hook_for :orm, :in => :rails, :as => :model, :required => true source_root File.expand_path('../templates', __FILE__) + class_option :with_image, :type => :boolean, :default => false + def create_tandem_resource_controller template 'controller.rb', File.join('app/controllers', "#{controller_file_name}_controller.rb") end @@ -27,6 +29,29 @@ def create_views def add_resource_route route "resources :#{file_name.pluralize}" end + + def add_image_to_model + if options.with_image + insert_into_file "app/models/#{file_name}.rb", :before => "end" do + content = "" + content += " attr_accessible :image, :image_delete\n\n" + content += " has_attached_file :image, Tandem::Configuration.paperclip_options\n" + content += " validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png)\n\n" + content += " before_save :destroy_image?\n\n" + content += " def image_delete\n" + content += " @image_delete ||= '0'\n" + content += " end\n\n" + content += " def image_delete=(value)\n" + content += " @image_delete = value\n" + content += " end\n\n" + content += " private\n\n" + content += " def destroy_image?\n" + content += " self.image.clear if @image_delete == '1'\n" + content += " end\n" + end + generate "paperclip #{file_name} image" + end + end end end end diff --git a/lib/generators/tandem/resource/templates/_form.html.slim b/lib/generators/tandem/resource/templates/_form.html.slim index 31add79..8bf502c 100644 --- a/lib/generators/tandem/resource/templates/_form.html.slim +++ b/lib/generators/tandem/resource/templates/_form.html.slim @@ -1,4 +1,4 @@ -= form_for @<%= singular_table_name %> do |f| += form_for @<%= singular_table_name %>, :html => { :multipart => true } do |f| - if @<%= singular_table_name %>.errors.any? #error_explanation h2 #{pluralize(@<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved: @@ -14,5 +14,16 @@ = f.<%= attribute.field_type %> :<%= attribute.name %> <% end -%> +<% if options.with_image -%> + .field + = f.label "Image" + = f.file_field :image + br + - if @<%= file_name %>.image? + = image_tag @<%= file_name %>.image.url + = f.label "Delete Image" + = f.check_box :image_delete +<% end -%> + .actions = f.submit diff --git a/lib/generators/tandem/resource/templates/show.html.slim b/lib/generators/tandem/resource/templates/show.html.slim index 81e4bde..1945fb9 100644 --- a/lib/generators/tandem/resource/templates/show.html.slim +++ b/lib/generators/tandem/resource/templates/show.html.slim @@ -5,3 +5,9 @@ p strong <%= attribute.human_name %>: = @<%= singular_table_name %>.<%= attribute.name %> <% end -%> + +<% if options.with_image -%> +- if @<%= file_name %>.image? + p + = image_tag @<%= file_name %>.image.url +<% end -%> diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 513a1e0..cca7500 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -45,6 +45,8 @@ t.datetime "resource_updated_at" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "imageable_id" + t.string "imageable_type" end create_table "tandem_pages", :force => true do |t| From 1e0d1afa2f6b38b22b75df4625547f4bdfb7eca0 Mon Sep 17 00:00:00 2001 From: JKHiggins Date: Fri, 21 Feb 2014 08:57:43 -0600 Subject: [PATCH 2/4] Specs for adding images to the resource via command line --- .../resource/resource_generator_spec.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/generators/tandem/resource/resource_generator_spec.rb b/spec/generators/tandem/resource/resource_generator_spec.rb index 9df340d..a7a4b04 100644 --- a/spec/generators/tandem/resource/resource_generator_spec.rb +++ b/spec/generators/tandem/resource/resource_generator_spec.rb @@ -70,4 +70,24 @@ it { should be_a_migration } end + + describe '#add_image_to_model' do + context 'no image flag on command line' do + before do + run_generator %w(Spoke) + end + + subject { file('app/models/spoke.rb') } + it { should_not contain(/has_attached_file :image/) } + end + + context 'image flag on command line' do + before do + run_generator %w(Spoke --with_image) + end + + subject { file('app/models/spoke.rb') } + it { should contain(/has_attached_file :image/) } + end + end end From 6499dc98c0f902df37820ecbfd1e8357da9a48b1 Mon Sep 17 00:00:00 2001 From: JKHiggins Date: Fri, 21 Feb 2014 10:43:01 -0600 Subject: [PATCH 3/4] Added more specs for adding image handling to files -Changed _form template to only have html multipart if we have an image included(now done via generator) --- .../tandem/resource/resource_generator.rb | 4 ++ .../tandem/resource/templates/_form.html.slim | 2 +- .../resource/resource_generator_spec.rb | 42 +++++++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/generators/tandem/resource/resource_generator.rb b/lib/generators/tandem/resource/resource_generator.rb index adabaad..58456a7 100644 --- a/lib/generators/tandem/resource/resource_generator.rb +++ b/lib/generators/tandem/resource/resource_generator.rb @@ -49,6 +49,10 @@ def add_image_to_model content += " self.image.clear if @image_delete == '1'\n" content += " end\n" end + insert_into_file "app/views/#{file_name}s/_form.html.slim", :after => "form_for @#{file_name}" do + content = "" + content += ", :html => { :multipart => true } " + end generate "paperclip #{file_name} image" end end diff --git a/lib/generators/tandem/resource/templates/_form.html.slim b/lib/generators/tandem/resource/templates/_form.html.slim index 8bf502c..bf990b3 100644 --- a/lib/generators/tandem/resource/templates/_form.html.slim +++ b/lib/generators/tandem/resource/templates/_form.html.slim @@ -1,4 +1,4 @@ -= form_for @<%= singular_table_name %>, :html => { :multipart => true } do |f| += form_for @<%= singular_table_name %> do |f| - if @<%= singular_table_name %>.errors.any? #error_explanation h2 #{pluralize(@<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved: diff --git a/spec/generators/tandem/resource/resource_generator_spec.rb b/spec/generators/tandem/resource/resource_generator_spec.rb index a7a4b04..3b9d372 100644 --- a/spec/generators/tandem/resource/resource_generator_spec.rb +++ b/spec/generators/tandem/resource/resource_generator_spec.rb @@ -77,8 +77,25 @@ run_generator %w(Spoke) end - subject { file('app/models/spoke.rb') } - it { should_not contain(/has_attached_file :image/) } + it 'should not add image handling to the model' do + text = file('app/models/spoke.rb') + text.should_not contain(/has_attached_file :image/) + end + + it 'should not add image handling to the show page' do + text = file('app/views/spokes/show.html.slim') + text.should_not contain(/image_tag @spoke.image.url/) + end + + it 'should not add image handling to the edit form' do + text = file('app/views/spokes/_form.html.slim') + text.should_not contain(/f.file_field :image/) + end + + it 'should not add html flag to _form.html.slim' do + text = file('app/views/spokes/_form.html.slim') + text.should_not contain(/:html => { :multipart => true }/) + end end context 'image flag on command line' do @@ -86,8 +103,25 @@ run_generator %w(Spoke --with_image) end - subject { file('app/models/spoke.rb') } - it { should contain(/has_attached_file :image/) } + it 'should add image handling to the model' do + text = file('app/models/spoke.rb') + text.should contain(/has_attached_file :image/) + end + + it 'should add image handling to the show page' do + text = file('app/views/spokes/show.html.slim') + text.should contain(/image_tag @spoke.image.url/) + end + + it 'should add image handling to the edit form' do + text = file('app/views/spokes/_form.html.slim') + text.should contain(/f.file_field :image/) + end + + it 'should add html flag to _form.html.slim' do + text = file('app/views/spokes/_form.html.slim') + text.should contain(/:html => { :multipart => true }/) + end end end end From 9fac15af72263b4124a1cee326527622555b8d83 Mon Sep 17 00:00:00 2001 From: JKHiggins Date: Sat, 22 Feb 2014 13:29:11 -0600 Subject: [PATCH 4/4] Updated to README to explain the addition of an image to a tandem resource --- README.rdoc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index ffa520b..8a00b25 100644 --- a/README.rdoc +++ b/README.rdoc @@ -209,7 +209,19 @@ content in the application. To generate a tandem resource, simply call the tandem_resource generator: rails generate tandem:resource Faq question:string answer:string position:integer - + +To generate a tandem resource with an image, append --with_image to the end of your generator line + + rails generate tandem:resource Faq question:string answer:string position:integer --with_image + +If you want to see the resource in the browser you must migrate the database as the generator will +create a migration for both the image and the resource itself. + +Navigating to the resource is then as easy as navigating to your app and appending the pluralized version +of the name of the resource to the url. In the case of the current "Faq" example it would be: + + www.app-url.com/faqs + == Contributing 1. Fork it.