Skip to content

Commit

Permalink
Refactored the association between pictures, recipes and albums to be…
Browse files Browse the repository at this point in the history
… polymorphic.
  • Loading branch information
quattro004 committed May 25, 2013
1 parent b3930c5 commit 5a41159
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 48 deletions.
17 changes: 7 additions & 10 deletions app/controllers/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,13 @@ def update

# Since both albums and recipes contain pictures we need some special redirect/response logic.
def respond_or_redirect
unless (@picture.valid?)
respond_with(@picture) # if the picture isn't valid then the user needs to try again
else
if (@picture.album_id != nil && @picture.album_id > 0)
redirect_to(album_path(@picture.album_id))
elsif (@picture.recipe_id != nil && @picture.recipe_id > 0)
redirect_to(recipe_path(@picture.recipe_id))
else
respond_with(@picture)
end
if (@picture.imageable_type == 'Album')
redirect_to(album_path(@picture.imageable_id))
return
elsif (@picture.imageable_type == 'Recipe')
redirect_to(recipe_path(@picture.imageable_id))
return
end
respond_with(@picture)
end
end
4 changes: 2 additions & 2 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def edit

def create
@recipe.user_id = user_signed_in? ? current_user.id : 0
@recipe.picture.delete_all if @recipe.picture.size > 0 && !@recipe.picture[0].data
@recipe.pictures.delete_all if @recipe.pictures.size > 0 && !@recipe.pictures[0].data
flash[:notice] = 'Recipe was successfully created.' if @recipe.save
respond_with(@recipe)
end
Expand Down Expand Up @@ -59,7 +59,7 @@ def get_author_name

def setup_defaults
3.times { @recipe.ingredient.build }
# @recipe.picture.build
@recipe.pictures.build
@recipe.cook_time_in_minutes = 0
@recipe.prep_time_in_minutes = 0
@recipe.is_public = false
Expand Down
2 changes: 1 addition & 1 deletion app/models/album.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Album < ActiveRecord::Base
has_one :album_type
belongs_to :user
has_many :picture
has_many :pictures, :as => :imageable
attr_accessible :description, :name, :user_id, :album_type_id

validates_presence_of :name
Expand Down
6 changes: 2 additions & 4 deletions app/models/picture.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class Picture < ActiveRecord::Base
belongs_to :recipe
belongs_to :user
belongs_to :album
attr_accessible :name, :content_type, :data, :size, :uploaded_picture, :description, :user_id, :album_id, :date_taken
belongs_to :imageable, :polymorphic => true
attr_accessible :name, :content_type, :data, :size, :uploaded_picture, :description, :user_id, :album_id, :date_taken, :imageable_id, :imageable_type

validate :picture_contains_data

Expand Down
12 changes: 6 additions & 6 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Recipe < ActiveRecord::Base
has_many :picture, :dependent => :destroy
has_many :ingredient, :dependent => :destroy
has_one :category
has_many :pictures, :as => :imageable, :dependent => :destroy
has_many :ingredient, :dependent => :destroy
has_one :category
belongs_to :user
attr_accessible :title, :instructions, :prep_time_in_minutes, :cook_time_in_minutes, :comment, :keywords, :is_public,
:is_secret, :yield, :original_source, :ingredient_attributes, :picture_attributes, :category_id, :preparation
:is_secret, :yield, :original_source, :ingredient_attributes, :pictures_attributes, :category_id, :preparation

accepts_nested_attributes_for :ingredient, :picture, :allow_destroy => true
accepts_nested_attributes_for :ingredient, :pictures, :allow_destroy => true

validates_presence_of :title
validates_presence_of :instructions
Expand All @@ -20,7 +20,7 @@ class Recipe < ActiveRecord::Base
private

def number_of_pictures?
errors.add :recipe, 'can only have a maximum of 4 pictures' unless picture.size < 5
errors.add :recipe, 'can only have a maximum of 4 pictures' unless pictures.size < 5
end

def number_of_ingredients?
Expand Down
5 changes: 2 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class User < ActiveRecord::Base
has_many :recipe
has_many :picture
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
has_many :pictures, :as => :imageable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :approved
validates_presence_of :name, :email
before_create :ensure_default_role
Expand Down
4 changes: 2 additions & 2 deletions app/views/albums/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<%= render 'form' %>
<%= link_to(new_picture_path(:album_id => @album.id), class: "btn btn-small btn-success") do %>
<%= link_to(new_picture_path(:imageable_id => @album.id, :imageable_type => 'Album'), class: "btn btn-small btn-success") do %>
<i class="icon-plus icon-white"></i> Add <%= @current_album_type.name.singularize %>
<% end %>
<%= link_to(@album, class: "btn btn-small btn-success") do %>
<i class="icon-eye-open icon-white"></i> View
<% end %>
&nbsp;
<%= link_to(albums_path(:album_type => @current_album_type.id), class: "btn btn-success") do %>
<i class="icon-th-list icon-white"></i> List <%= @current_album_type.name.singularize %> Albums
<% end %>
6 changes: 3 additions & 3 deletions app/views/albums/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<% end %>
<% if @current_album_type.name.singularize == 'Picture' %>
<%= link_to(new_picture_path(:album_id => @album.id), class: "btn btn-large btn-success") do %>
<%= link_to(new_picture_path(:imageable_id => @album.id, :imageable_type => 'Album'), class: "btn btn-large btn-success") do %>
<i class="icon-plus icon-white"></i> Add <%= @current_album_type.name.singularize %>
<% end %>
<% end %>
Expand All @@ -30,7 +30,7 @@
<div id="myCarousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<% @album.picture.each_with_index do |picture, index| %>
<% @album.pictures.each_with_index do |picture, index| %>
<div class="<%= index == 0 ? 'active item' : 'item' %>">
<%= link_to(picture_path(picture), class: "thumbnail") do %>
<img
Expand All @@ -57,7 +57,7 @@
<% else %>
<label><em>(click image to view original size)</em></label>
<ul class="thumbnails">
<% @album.picture.each do |picture| %>
<% @album.pictures.each do |picture| %>
<li class="span4">
<div class="thumbnail">
<% if ((can? :edit, picture) && (picture.user_id == current_user.id || current_user.role?(:admin) )) %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/pictures/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<%= date_select("picture", "date_taken", :order => [:month, :day, :year], :start_year => 1930, :end_year => Time.now.year) %>
</div>

<%= f.hidden_field :album_id, :value => params[:album_id] %>
<%= f.hidden_field :imageable_id, :value => params[:imageable_id] %>
<%= f.hidden_field :imageable_type, :value => params[:imageable_type] %>

<div class="actions">
<%= f.submit nil, :class => 'btn btn-success btn-large' %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/recipes/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<label class="control-label">Pictures</label>
<% if action_name == 'edit' %>
<ul class="thumbnails">
<% for picture in @recipe.picture %>
<% for picture in @recipe.pictures %>
<li class="span1">
<div class="thumbnail">
<% if ((can? :destroy, picture) && (@recipe.user_id == current_user.id || current_user.role?(:admin) )) %>
Expand All @@ -123,12 +123,12 @@
<% end %>
</ul>
<% else %>
<%= f.fields_for :picture do |builder| %>
<%= f.fields_for :pictures do |builder| %>
<%= render 'picture_fields', :f => builder %>
<% end %>
<% end %>
<div class="controls">
<%= link_to_add_fields 'Add Picture', f, :picture %>
<%= link_to_add_fields 'Add Picture', f, :pictures %>
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<tbody>
<% for recipe in @recipes %>
<tr>
<% if recipe.picture[0] %>
<td><img src="<%= url_for(:action => 'thumbnail', :id => recipe.picture[0].id, :controller => 'pictures') %>" alt="Recipe thumbnail" /></td>
<% if recipe.pictures[0] %>
<td><img src="<%= url_for(:action => 'thumbnail', :id => recipe.pictures[0].id, :controller => 'pictures') %>" alt="Recipe thumbnail" /></td>
<% else %>
<td><%= image_tag('add_photo.png', :border => 0) %></td>
<% end %>
Expand Down
13 changes: 6 additions & 7 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
<small style="float: right;">Yield: <label class="text"><em><%= @recipe.yield %></em></label></small>
<% end %>
</div>

<% unless @recipe.picture.empty? %>
<% if @recipe.picture.size > 1 %>
<% unless @recipe.pictures.empty? %>
<% if @recipe.pictures.size > 1 %>
<label style="display:inline;"><h4>Pictures
<% @recipe.picture.each_with_index do |picture, index| %>
<% @recipe.pictures.each_with_index do |picture, index| %>
<strong><%= link_to index + 1, picture_path(picture) %></strong>
<% end %>
</h4>
</label>
<% end %>
<img src="<%= url_for(:action => 'thumbnail', :id => @recipe.picture[0].id, :controller => 'pictures', :height => 256, :width => 256) %>" />
<img src="<%= url_for(:action => 'thumbnail', :id => @recipe.pictures[0].id, :controller => 'pictures', :height => 256, :width => 256) %>" />
<% end %>
<% unless @recipe.comment.empty? %>
Expand Down Expand Up @@ -51,7 +50,7 @@
<ul>
<% for ingredient in @recipe.ingredient %>
<% unless ingredient.secret %>
<label class="text"><li><%= ingredient.amount %> <%= ingredient.name %></label></li>
<li><label class="text"><%= ingredient.amount %> <%= ingredient.name %></label></li>
<% end %>
<% end %>
</ul>
Expand All @@ -60,7 +59,7 @@
<label><h4>Instructions</h4>
<ol>
<% @recipe.instructions.split(/\r\n/).each do |instruction| %>
<label class="text"><li><%= instruction %></li></label>
<li><label class="text"><%= instruction %></label></li>
<% end %>
</ol>
</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddImageableAttributesToPictures < ActiveRecord::Migration
def change
add_column :pictures, :imageable_id, :integer
add_column :pictures, :imageable_type, :string
end
end
10 changes: 6 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130302164825) do
ActiveRecord::Schema.define(:version => 20130517130546) do

create_table "album_types", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -53,14 +53,16 @@
t.string "name"
t.string "content_type"
t.integer "size"
t.binary "data", :limit => 16777215
t.binary "data", :limit => 16777215
t.integer "recipe_id"
t.integer "user_id"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "album_id"
t.date "date_taken"
t.integer "imageable_id"
t.string "imageable_type"
end

add_index "pictures", ["album_id"], :name => "index_pictures_on_album_id"
Expand Down

0 comments on commit 5a41159

Please sign in to comment.