Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Colleen]Added images to tandem resource generator #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 32 additions & 3 deletions lib/generators/tandem/resource/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 11 additions & 0 deletions lib/generators/tandem/resource/templates/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions lib/generators/tandem/resource/templates/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
2 changes: 2 additions & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
54 changes: 54 additions & 0 deletions spec/generators/tandem/resource/resource_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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