Commit 10f3e395

morilla <mo@mokhan.ca>
2011-05-04 14:47:09
associate users with creations.
1 parent bc6d839
app/controllers/creations_controller.rb
@@ -1,4 +1,5 @@
 class CreationsController < ApplicationController
+  before_filter :authenticate_user!, :except => [:show, :index]
   # GET /creations
   # GET /creations.xml
   def index
@@ -34,7 +35,7 @@ class CreationsController < ApplicationController
 
   # GET /creations/1/edit
   def edit
-    @creation = Creation.find(params[:id])
+    @creation = current_user.creations.find(params[:id])
   end
 
   # POST /creations
@@ -56,7 +57,7 @@ class CreationsController < ApplicationController
   # PUT /creations/1
   # PUT /creations/1.xml
   def update
-    @creation = Creation.find(params[:id])
+    @creation = current_user.creations.find(params[:id])
 
     respond_to do |format|
       if @creation.update_attributes(params[:creation])
@@ -72,7 +73,7 @@ class CreationsController < ApplicationController
   # DELETE /creations/1
   # DELETE /creations/1.xml
   def destroy
-    @creation = Creation.find(params[:id])
+    @creation = current_user.creations.find(params[:id])
     @creation.destroy
 
     respond_to do |format|
@@ -80,4 +81,13 @@ class CreationsController < ApplicationController
       format.xml  { head :ok }
     end
   end
+
+  def mine
+    @creations = current_user.creations
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.xml  { render :xml => @creations }
+    end
+  end
 end
app/models/creation.rb
@@ -1,2 +1,3 @@
 class Creation < ActiveRecord::Base
+  belongs_to :user
 end
app/models/user.rb
@@ -6,4 +6,6 @@ class User < ActiveRecord::Base
 
   # Setup accessible (or protected) attributes for your model
   attr_accessible :email, :password, :password_confirmation, :remember_me
+
+  has_many :creations
 end
app/views/creations/index.html.erb
@@ -9,8 +9,10 @@
         <p><%= creation.story %></p>
         <p>
         <%= link_to 'more...', creation %>
-        <%= link_to 'edit', edit_creation_path(creation) %> |
-        <%= link_to 'destroy', creation, :confirm => 'Are you sure?', :method => :delete %>
+        <% if creation.user == current_user %>
+          <%= link_to 'edit', edit_creation_path(creation) %> |
+          <%= link_to 'destroy', creation, :confirm => 'Are you sure?', :method => :delete %>
+        <% end %>
         </p>
       </div>
     </div>
app/views/creations/mine.html.erb
@@ -0,0 +1,34 @@
+<div class="heading">The most recent creations</div>
+
+<div class="organic">
+<% @creations.reverse.each_with_index do |creation, index| %>
+    <div class="organicHolder">
+      <div class="organicContent">
+        <a href="#"><img src="/images/img02.jpg" alt="" /></a>
+        <h2><%= index %>. <%= creation.name %></h2>
+        <p><%= creation.story %></p>
+        <p>
+        <%= link_to 'more...', creation %>
+        <%= link_to 'edit', edit_creation_path(creation) %> |
+        <%= link_to 'destroy', creation, :confirm => 'Are you sure?', :method => :delete %>
+        </p>
+      </div>
+    </div>
+  <% if index % 3 == 1 %>
+</div>
+
+<div class="space"></div>
+<div class="organic">
+
+  <% end %>
+
+<% end %>
+</div>
+
+
+<div class="space"></div>
+
+<div class="clear"></div>
+
+<div class="hr"></div>
+<div class="space"></div>
app/views/home/index.html.erb
@@ -4,6 +4,7 @@
     <h1>Welcome to the CakeSide</h1>
     <p><em>Introduce your company here. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </em></p>  
     <p align="right"><input type="button" value="MY CREATIONS" class="view" /></p> 
+    <%= link_to "mine", my_creations_path %>
     <div class="clear"></div>
   </div>
   <div class="welcomeBottom"></div>
app/views/layouts/application.html.erb
@@ -3,7 +3,7 @@
 <head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
-  <title>Cake - <%= yield :title %></title>
+  <title><%= yield(:title) || "untitled" %> - CakeSide</title>
   <meta name="description" content="">
   <meta name="author" content="">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -11,9 +11,9 @@
   <script src="javascripts/libs/modernizr-1.7.min.js"></script>
   <%= stylesheet_link_tag :all %>
   <%= csrf_meta_tag %>
+  <%= yield :head %>
 </head>
 <body>
-
   <div id="main"><!-- Main starts here -->
     <header>
     <div id="header"><!-- Header starts here -->
@@ -85,8 +85,6 @@
     </div>
   </div><!-- Main ends here -->
 
-
-
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
   <%= javascript_include_tag :all %>
 
config/routes.rb
@@ -6,6 +6,7 @@ Cake::Application.routes.draw do
   get "home/index"
 
   devise_for :users
+  match 'my_creations' => 'creations#mine', :as => 'my_creations', :method => 'GET'
 
   # The priority is based upon order of creation:
   # first created -> highest priority.
db/migrate/20110504010209_add_user_id_to_creations.rb
@@ -0,0 +1,9 @@
+class AddUserIdToCreations < ActiveRecord::Migration
+  def self.up
+    add_column :creations, :user_id, :int
+  end
+
+  def self.down
+    remove_column :creations, :user_id
+  end
+end
db/schema.rb
@@ -10,13 +10,14 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20110503032142) do
+ActiveRecord::Schema.define(:version => 20110504010209) do
 
   create_table "creations", :force => true do |t|
     t.string   "name"
     t.text     "story"
     t.datetime "created_at"
     t.datetime "updated_at"
+    t.integer  "user_id"
   end
 
   create_table "users", :force => true do |t|