Skip to content

Commit 86a79c8

Browse files
committed
refactored layout and added questions features for creating questions attached to user
1 parent c65956f commit 86a79c8

23 files changed

Lines changed: 158 additions & 61 deletions

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ end
4141

4242
group :test do
4343
gem "mocha"
44+
gem "database_cleaner"
4445
end

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ GEM
6868
capybara (>= 1.0.0)
6969
cucumber (~> 1.0.0)
7070
nokogiri (>= 1.4.6)
71+
database_cleaner (0.6.7)
7172
devise (1.4.2)
7273
bcrypt-ruby (~> 2.1.2)
7374
orm_adapter (~> 0.0.3)
@@ -210,6 +211,7 @@ DEPENDENCIES
210211
cancan
211212
coffee-rails (~> 3.1.0.rc)
212213
cucumber-rails
214+
database_cleaner
213215
devise (>= 1.2)
214216
fabrication
215217
faker
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class QuestionsController < InheritedController
2-
3-
def index
4-
@question = Question.new
5-
end
2+
load_and_authorize_resource
63

74
def create
8-
create!(:notice => "Question Asked!"){collection_url}
5+
@question = Question.create params[:question]
6+
@question.user = current_user
7+
create!(:notice => "Question Asked!"){ collection_url }
98
end
109
end

app/models/ability.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
class Ability
22
include CanCan::Ability
33

4-
def initialize(user)
4+
def initialize(user)
5+
6+
can :read, Question
7+
8+
if user
9+
can :create, Question
10+
can :manage, Question, :user => user
11+
end
12+
513
# Define abilities for the passed in user here. For example:
614
#
715
# user ||= User.new # guest user (not logged in)

app/models/question.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ class Question
44
key :title, String
55
key :description, String
66

7+
belongs_to :user
8+
79
end

app/views/layouts/application.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
- if flash[:notice]
1414
.flash #{flash[:notice]}
1515

16+
%h1.title
17+
= yield :title
18+
1619
= yield :links
1720
= yield

app/views/questions/edit.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- content_for :title do
2-
= "Editing #{resource}"
2+
= "Editing #{resource.title}"
33

44
= render 'form'

app/views/questions/index.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
- content_for :links do
55
- if can? :create, Question
6-
= link_to "New question", new_resource_path
6+
= link_to "Ask a Question", new_resource_path
77

88
%table
99
%thead

app/views/questions/show.html.haml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
- content_for :title do
2-
= resource
2+
= resource.title
33

44
- content_for :links do
55
- if can? :update, resource
66
= link_to 'Edit', edit_resource_path
77
- if can? :destroy, resource
88
= link_to 'Delete', resource_path, :confirm => 'Are you sure?', :method => :delete
99

10-
%dl
11-
%dt Title
12-
%dd= resource.title
13-
%dt Description
14-
%dd= resource.description
10+
- if resource.user
11+
.author Created by #{resource.user.username}
12+
13+
.description
14+
= resource.description
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= semantic_menu do |root|
22
- if current_user
3-
= current_user.email
4-
- root.add "Log Out", destroy_user_session_path, :method => :delete
3+
= current_user.username
4+
- root.add "Log Out", logout_path
55
- else
6-
- root.add "Log In", new_user_session_path
6+
- root.add "Log In", login_path

0 commit comments

Comments
 (0)