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. diff --git a/lib/generators/tandem/resource/resource_generator.rb b/lib/generators/tandem/resource/resource_generator.rb index e3a5d1f..58456a7 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,33 @@ 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 + 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 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..bf990b3 100644 --- a/lib/generators/tandem/resource/templates/_form.html.slim +++ b/lib/generators/tandem/resource/templates/_form.html.slim @@ -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| diff --git a/spec/generators/tandem/resource/resource_generator_spec.rb b/spec/generators/tandem/resource/resource_generator_spec.rb index 9df340d..3b9d372 100644 --- a/spec/generators/tandem/resource/resource_generator_spec.rb +++ b/spec/generators/tandem/resource/resource_generator_spec.rb @@ -70,4 +70,58 @@ 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 + + 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 + before do + run_generator %w(Spoke --with_image) + end + + 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