Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished relationships and fixed mass assignment issue

  • Loading branch information...
commit 8aff57aa1ee5868750049f0612bc824d753b353a 1 parent 0ef0f9e
@cpursley authored
View
2  Gemfile
@@ -39,4 +39,4 @@ gem 'jquery-rails'
# To use debugger
# gem 'debugger'
-# git checkout -b routes-layouts
+# git checkout -b relation-validation
View
28 app/controllers/dreams_controller.rb
@@ -1,10 +1,11 @@
class DreamsController < ApplicationController
- before_filter :authenticate_user!, :except => [:index, :show]
+ before_filter :authenticate_user!, :except => [:index, :all, :show, :about]
+ rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
# GET /dreams
# GET /dreams.json
def index
- @dreams = Dream.all
+ @dreams = Dream.where(:state => '1') # public's view - all published articles
respond_to do |format|
format.html # index.html.erb
@@ -12,6 +13,14 @@ def index
end
end
+ def my
+ @dreams = Dream.where(:state => ['0', '1']) # user's view - private and published articles
+ respond_to do |format|
+ format.html { render 'index' }
+ format.json { render json: @dreams }
+ end
+ end
+
# GET /dreams/1
# GET /dreams/1.json
def show
@@ -26,8 +35,8 @@ def show
# GET /dreams/new
# GET /dreams/new.json
def new
- @dream = Dream.new
-
+ @dream = current_user.dreams.new
+
respond_to do |format|
format.html # new.html.erb
format.json { render json: @dream }
@@ -36,13 +45,13 @@ def new
# GET /dreams/1/edit
def edit
- @dream = Dream.find(params[:id])
+ @dream = current_user.dreams.find(params[:id])
end
# POST /dreams
# POST /dreams.json
def create
- @dream = Dream.new(params[:dream])
+ @dream = current_user.dreams.new(params[:dream])
respond_to do |format|
if @dream.save
@@ -58,7 +67,7 @@ def create
# PUT /dreams/1
# PUT /dreams/1.json
def update
- @dream = Dream.find(params[:id])
+ @dream = current_user.dreams.find(params[:id])
respond_to do |format|
if @dream.update_attributes(params[:dream])
@@ -74,7 +83,7 @@ def update
# DELETE /dreams/1
# DELETE /dreams/1.json
def destroy
- @dream = Dream.find(params[:id])
+ @dream = current_user.dreams.find(params[:id])
@dream.destroy
respond_to do |format|
@@ -82,4 +91,7 @@ def destroy
format.json { head :no_content }
end
end
+
+ def about
+ end
end
View
21 app/models/dream.rb
@@ -1,3 +1,20 @@
class Dream < ActiveRecord::Base
- attr_accessible :accepted, :body, :changelog, :freezebody, :message, :state, :submitted, :teaser, :title, :user_id, :version
-end
+belongs_to :user
+
+ attr_accessible :title, :teaser, :body, :version, :changelog, :user_id, :message, :freezebody, :state, :submitted, :accepted
+
+ validates :user_id, :presence => true
+ validates :title, :presence => true, :length => { :maximum => 80 }
+ validates :teaser, :presence => true, :length => { :maximum => 500 }
+ validates :body, :presence => true
+ validates :version, :length => { :maximum => 120 }
+ validates :changelog, :length => { :maximum => 2000 }
+ validates :message, :length => { :maximum => 5000 }
+ validates :state, :presence => true, :numericality => true, :inclusion => { :in => 0..1 }
+
+ protected
+ def record_not_found
+ flash[:error] = 'The dream you requested could not be found.'
+ redirect_to root_url
+ end
+end
View
6 app/models/user.rb
@@ -5,7 +5,9 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
+ has_many :dreams, :dependent => :destroy
+
# Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation, :remember_me
+attr_accessible :email, :password, :password_confirmation, :remember_me, :fullname, :shortbio, :weburl
# attr_accessible :title, :body
-end
+end
View
4 app/views/layouts/_header.html.erb
@@ -5,8 +5,8 @@
<nav>
<ul class="nav pull-right">
<li><%= link_to "About", about_dreams_path %></li>
- <li><%= link_to "Featured Dreams", root_path %></li>
- <li><%= link_to "All Dreams", all_dreams_path %></li>
+ <li><%= link_to "Dreams", root_path %></li>
+ <li><%= link_to "My Dreams", my_dreams_path %></li>
<% if user_signed_in? %>
<li><%= link_to "My Profile", edit_user_registration_path %></li>
<li><%= link_to "Sign out", destroy_user_session_path %></li>
View
2  config/routes.rb
@@ -4,7 +4,7 @@
resources :dreams do
collection do
get 'about'
- get 'all'
+ get 'my'
end
end
View
10 db/migrate/20121118231059_create_dreams.rb
@@ -1,15 +1,15 @@
class CreateDreams < ActiveRecord::Migration
def change
create_table :dreams do |t|
- t.integer :user_id
- t.string :title
- t.text :teaser
- t.text :body
+ t.integer :user_id, :null => false
+ t.string :title, :null => false
+ t.text :teaser, :null => false
+ t.text :body, :null => false
t.string :version
t.text :changelog
t.string :message
t.text :freezebody
- t.integer :state
+ t.integer :state, :null => false, :default => 0 # 0 = private dream, 1 = public dream
t.date :submitted
t.date :accepted
View
6 db/migrate/20121118232125_devise_create_users.rb
@@ -33,10 +33,14 @@ def change
## Token authenticatable
# t.string :authentication_token
+ # author information
+ t.string :fullname
+ t.text :shortbio
+ t.string :weburl
t.timestamps
end
-
+ add_index :users, :fullname
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
View
18 db/schema.rb
@@ -14,19 +14,19 @@
ActiveRecord::Schema.define(:version => 20121118232125) do
create_table "dreams", :force => true do |t|
- t.integer "user_id"
- t.string "title"
- t.text "teaser"
- t.text "body"
+ t.integer "user_id", :null => false
+ t.string "title", :null => false
+ t.text "teaser", :null => false
+ t.text "body", :null => false
t.string "version"
t.text "changelog"
t.string "message"
t.text "freezebody"
- t.integer "state"
+ t.integer "state", :default => 0, :null => false
t.date "submitted"
t.date "accepted"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
@@ -40,11 +40,15 @@
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
+ t.string "fullname"
+ t.text "shortbio"
+ t.string "weburl"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["fullname"], :name => "index_users_on_fullname"
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
Please sign in to comment.
Something went wrong with that request. Please try again.