Skip to content

Commit

Permalink
Added ability to link photos to sites or projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cottman-Fields committed Oct 19, 2012
1 parent a50fb60 commit 5051eaf
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 210 deletions.
172 changes: 89 additions & 83 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,83 +1,89 @@
class PhotosController < ApplicationController
# GET /photos
# GET /photos.json
def index
@photos = Photo.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @photos }
end
end

# GET /photos/1
# GET /photos/1.json
def show
@photo = Photo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @photo }
end
end

# GET /photos/new
# GET /photos/new.json
def new
@photo = Photo.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @photo }
end
end

# GET /photos/1/edit
def edit
@photo = Photo.find(params[:id])
end

# POST /photos
# POST /photos.json
def create
@photo = Photo.new(params[:photo])

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render json: @photo, status: :created, location: @photo }
else
format.html { render action: "new" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# PUT /photos/1
# PUT /photos/1.json
def update
@photo = Photo.find(params[:id])

respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /photos/1
# DELETE /photos/1.json
def destroy
@photo = Photo.find(params[:id])
@photo.destroy

respond_to do |format|
format.html { redirect_to photos_url }
format.json { head :no_content }
end
end
end
class PhotosController < ApplicationController
# GET /photos
# GET /photos.json
def index
@photos = Photo.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @photos }
end
end

# GET /photos/1
# GET /photos/1.json
def show
@photo = Photo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @photo }
end
end

# GET /photos/new
# GET /photos/new.json
def new
@photo = Photo.new

@all_sites = Site.all
@all_projects = Project.all

respond_to do |format|
format.html # new.html.erb
format.json { render json: @photo }
end
end

# GET /photos/1/edit
def edit
@photo = Photo.find(params[:id])

@all_sites = Site.all
@all_projects = Project.all
end

# POST /photos
# POST /photos.json
def create
@photo = Photo.new(params[:photo])

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render json: @photo, status: :created, location: @photo }
else
format.html { render action: "new" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# PUT /photos/1
# PUT /photos/1.json
def update
@photo = Photo.find(params[:id])

respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /photos/1
# DELETE /photos/1.json
def destroy
@photo = Photo.find(params[:id])
@photo.destroy

respond_to do |format|
format.html { redirect_to photos_url }
format.json { head :no_content }
end
end
end
20 changes: 12 additions & 8 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :copyright, :uri, :description

validates :uri, :presence => true
validates_format_of :uri, :with => URI::regexp(%w(http https))
validates :copyright, :presence => true
end
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :copyright, :uri, :description, :imageable_type, :imageable_id

# http://stackoverflow.com/questions/6778269/rails-3-polymorphic-liking-of-entities-by-user-how
# http://stackoverflow.com/questions/746387/labels-for-radio-buttons-in-rails-form
validates :uri, :presence => true
validates_format_of :uri, :with => URI::regexp(%w(http https))
validates :copyright, :presence => true


end
65 changes: 40 additions & 25 deletions app/views/photos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
<%= form_for(@photo) do |f| %>
<% if @photo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:</h2>

<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :uri %><br />
<%= f.text_field :uri %>
</div>
<div class="field">
<%= f.label :copyright %><br />
<%= f.text_area :copyright %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_for(@photo) do |f| %>
<% if @photo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:</h2>

<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :uri %><br />
<%= f.text_field :uri %>
</div>
<div class="field">
<%= f.label :copyright %><br />
<%= f.text_field :copyright %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% @all_sites.each do |site| %>
<input id="photo_site_id_<%= site.id %>" name="photo[imageable_id]" type="radio" value="<%= site.id %>" <%= (@photo.imageable_id == site.id && @photo.imageable_type == 'Site') ? 'checked' : '' %>>
<label for="photo_site_id_<%= site.id %>"><%= site.name %></label>
<input id="photo_site_type_<%= site.id %>" name="photo[imageable_type]" type="radio" value="Site" <%= (@photo.imageable_id == site.id && @photo.imageable_type == 'Site') ? 'checked' : '' %>>
<label for="photo_site_type_<%= site.id %>">Site</label><br>
<% end %>
<% @all_projects.each do |project| %>
<input id="photo_project_id_<%= project.id %>" name="photo[imageable_id]" type="radio" value="<%= project.id %>" <%= (@photo.imageable_id == project.id && @photo.imageable_type == 'Project') ? 'checked' : '' %>>
<label for="photo_project_id_<%= project.id %>"><%= project.name %></label>
<input id="photo_project_type_<%= project.id %>" name="photo[imageable_type]" type="radio" value="Project" <%= (@photo.imageable_id == project.id && @photo.imageable_type == 'Project') ? 'checked' : '' %>>
<label for="photo_project_type_<%= project.id %>">project</label><br>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
56 changes: 31 additions & 25 deletions app/views/photos/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
<h1>Listing photos</h1>

<table>
<tr>
<th>Uri</th>
<th>Copyright</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @photos.each do |photo| %>
<tr>
<td><%= photo.uri %></td>
<td><%= photo.copyright %></td>
<td><%= link_to 'Show', photo %></td>
<td><%= link_to 'Edit', edit_photo_path(photo) %></td>
<td><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Photo', new_photo_path %>
<h1>Listing photos</h1>

<table>
<tr>
<th>Uri</th>
<th>Copyright</th>
<th>Description</th>
<th>Ref Id</th>
<th>Ref Table</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @photos.each do |photo| %>
<tr>
<td><%= photo.uri %></td>
<td><%= photo.copyright %></td>
<td><%= photo.description %></td>
<td><%= photo.imageable_id %></td>
<td><%= photo.imageable_type %></td>
<td><%= link_to 'Show', photo %></td>
<td><%= link_to 'Edit', edit_photo_path(photo) %></td>
<td><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Photo', new_photo_path %>
39 changes: 24 additions & 15 deletions app/views/photos/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<p id="notice"><%= notice %></p>

<p>
<b>Uri:</b>
<%= @photo.uri %>
</p>

<p>
<b>Copyright:</b>
<%= @photo.copyright %>
</p>


<%= link_to 'Edit', edit_photo_path(@photo) %> |
<%= link_to 'Back', photos_path %>
<p id="notice"><%= notice %></p>

<p>
<b>Uri:</b>
<%= @photo.uri %>
</p>

<p>
<b>Copyright:</b>
<%= @photo.copyright %>
</p>

<p>
<b>Description:</b>
<%= @photo.description %>
</p>

<p>
<b>Parent:</b>
<%= @photo.imageable %>
</p>

<%= link_to 'Edit', edit_photo_path(@photo) %> |
<%= link_to 'Back', photos_path %>
Loading

0 comments on commit 5051eaf

Please sign in to comment.