Permalink
Browse files

Task 5

  • Loading branch information...
1 parent b8ec861 commit 6fe0e7eaa8b74885a971fee349a8d255ce635681 @varid007 committed May 6, 2012
Showing with 361 additions and 16 deletions.
  1. +2 −0 belajar/Gemfile
  2. +3 −0 belajar/Gemfile.lock
  3. +3 −0 belajar/app/assets/javascripts/comments.js.coffee
  4. +3 −0 belajar/app/assets/javascripts/sessions.js.coffee
  5. +3 −0 belajar/app/assets/javascripts/users.js.coffee
  6. +3 −0 belajar/app/assets/stylesheets/comments.css.scss
  7. +3 −0 belajar/app/assets/stylesheets/sessions.css.scss
  8. +3 −0 belajar/app/assets/stylesheets/users.css.scss
  9. +18 −0 belajar/app/controllers/application_controller.rb
  10. +29 −9 belajar/app/controllers/articles_controller.rb
  11. +16 −0 belajar/app/controllers/comments_controller.rb
  12. +21 −0 belajar/app/controllers/sessions_controller.rb
  13. +21 −0 belajar/app/controllers/users_controller.rb
  14. +11 −0 belajar/app/helpers/application_helper.rb
  15. +2 −0 belajar/app/helpers/comments_helper.rb
  16. +2 −0 belajar/app/helpers/sessions_helper.rb
  17. +2 −0 belajar/app/helpers/users_helper.rb
  18. +8 −0 belajar/app/mailers/user_mailer.rb
  19. +28 −3 belajar/app/models/user.rb
  20. +1 −1 belajar/app/views/articles/edit.html.erb
  21. +2 −2 belajar/app/views/articles/index.html.erb
  22. +4 −0 belajar/app/views/articles/new.html.erb
  23. +14 −0 belajar/app/views/articles/show.html.erb
  24. +4 −0 belajar/app/views/layouts/application.html.erb
  25. +13 −0 belajar/app/views/sessions/new.html.erb
  26. +5 −0 belajar/app/views/user_mailer/registration_confirmation.html.erb
  27. +42 −0 belajar/app/views/users/new.html.erb
  28. +13 −0 belajar/config/environments/development.rb
  29. +2 −0 belajar/config/initializers/recaptcha.rb
  30. +15 −0 belajar/config/routes.rb
  31. +6 −0 belajar/db/migrate/20120504060927_add_pass_to_user.rb
  32. +5 −0 belajar/db/migrate/20120506143215_add_userid_toarticle.rb
  33. +5 −0 belajar/db/migrate/20120506143242_add_articles_tocomment.rb
  34. +5 −1 belajar/db/schema.rb
  35. +7 −0 belajar/test/functional/comments_controller_test.rb
  36. +9 −0 belajar/test/functional/sessions_controller_test.rb
  37. +7 −0 belajar/test/functional/user_mailer_test.rb
  38. +9 −0 belajar/test/functional/users_controller_test.rb
  39. +4 −0 belajar/test/unit/helpers/comments_helper_test.rb
  40. +4 −0 belajar/test/unit/helpers/sessions_helper_test.rb
  41. +4 −0 belajar/test/unit/helpers/users_helper_test.rb
View
2 belajar/Gemfile
@@ -8,6 +8,8 @@ gem 'rails', '3.1.0'
gem 'mysql2'
gem 'execjs'
gem 'therubyracer'
+gem 'bcrypt-ruby', :require => 'bcrypt'
+gem 'recaptcha', :require => 'recaptcha/rails'
# Gems used only for assets and not required
# in production environments by default.
View
3 belajar/Gemfile.lock
@@ -86,6 +86,7 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ recaptcha (0.3.4)
sass (3.1.16)
sass-rails (3.1.6)
actionpack (~> 3.1.0)
@@ -114,11 +115,13 @@ PLATFORMS
ruby
DEPENDENCIES
+ bcrypt-ruby
coffee-rails (~> 3.1.0)
execjs
jquery-rails
mysql2
rails (= 3.1.0)
+ recaptcha
sass-rails (~> 3.1.0)
therubyracer
turn
View
3 belajar/app/assets/javascripts/comments.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 belajar/app/assets/javascripts/sessions.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 belajar/app/assets/javascripts/users.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 belajar/app/assets/stylesheets/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3 belajar/app/assets/stylesheets/sessions.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3 belajar/app/assets/stylesheets/users.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
18 belajar/app/controllers/application_controller.rb
@@ -1,3 +1,21 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ helper_method :current_user
+
+ def require_login
+ if current_user.nil?
+ flash[:error] = "You are not permited, please login first"
+ redirect_to new_session_path
+ else
+ return current_user
+ end
+ end
+
+ private
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ end
+
+
end
View
38 belajar/app/controllers/articles_controller.rb
@@ -1,11 +1,16 @@
class ArticlesController < ApplicationController
+ before_filter :require_login, :only => [:new ,:edit, :delete, :update,:destroy]
+ before_filter :your_article, :only => [:edit, :delete, :update,:destroy]
+ before_filter :find_article, :only => [:show, :edit, :delete, :update,:destroy]
def index
- @articles = Article.all
+ @article = Article.all
end
def create
- @article = Article.new(params[:article])
+
+ @article=Article.new(params[:article])
+
if @article.save
flash[:notice] = 'Article was succesful create'
redirect_to articles_path
@@ -21,31 +26,46 @@ def new
end
def edit
- @article = Article.find_by_id(params[:id])
+
end
def show
- @article = Article.find_by_id(params[:id])
-
+ @comments=@article.comments
+ @comment=@article.comments.new
end
def update
- @article = Article.find(params[:id])
- if @article.update_attributes(params[:article])
+ if @article.update_attributes(params[:article])
+ flash[:notice] = 'Article was succesful Update'
redirect_to articles_path
else
- flash[:notice] = 'User was Error.'
+
render :edit
end
end
def destroy
- @article = Article.find_by_id(params[:id])
@article.destroy
redirect_to articles_path
end
+ def find_article
+ @article = Article.find_by_id(params[:id])
+ if @article.nil?
+ flash[:notice] = 'Error Detected'
+ redirect_to :controller => :articles, :action =>"new"
+ end
+ end
+
+ def your_article
+ user=User.find_by_email(current_user.email)
+ unless @article.user_id==user.id
+ flash[:notice]="its not your article"
+ redirect_to articles_path
+
+ end
+ end
end
View
16 belajar/app/controllers/comments_controller.rb
@@ -0,0 +1,16 @@
+class CommentsController < ApplicationController
+ def new
+ @comment=Comment.new
+ redirect_to :controller => :articles, :action => :show
+ end
+
+ def create
+ @comment = Comment.new(params[:comment])
+ # @article = Article.find_by_id(params[:comment])
+ if @comment.save
+ flash[:notice]= "Comment posted"
+ redirect_to articles_path
+ end
+
+ end
+end
View
21 belajar/app/controllers/sessions_controller.rb
@@ -0,0 +1,21 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.authenticate(params[:email], params[:password])
+ if user
+ session[:user_id] = user.id
+ redirect_to articles_path :notice => "Logged in!"
+ else
+ flash[:notice] = "Invalid email or password"
+ render "new"
+ end
+ end
+
+ def destroy
+ session[:user_id]=nil
+ redirect_to new_session_path :notice => "logged out !"
+
+ end
+end
View
21 belajar/app/controllers/users_controller.rb
@@ -0,0 +1,21 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if verify_recaptcha
+ if @user.save
+ UserMailer.registration_confirmation(@user).deliver
+ flash[:notice] = 'Account was Created, Log in ?'
+ redirect_to :controller => :sessions, :action => :new, :notice => "Signed up!"
+ else
+ render :new
+ end
+ else
+ flash[:error]= "There was an error with the recaptcha code"
+ render :new
+ end
+ end
+end
View
11 belajar/app/helpers/application_helper.rb
@@ -1,2 +1,13 @@
module ApplicationHelper
+
+def welcome_text
+ str = "" #jika user telah login, tampilkan welcome textnya
+ if current_user
+ str = "Welcome, #{current_user.email} | "
+ str += link_to "Logout", log_out_path
+ else
+ str = "#{link_to "Login", log_in_path} | "
+ str += link_to "Signup", sign_up_path
+ end
+end
end
View
2 belajar/app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
View
2 belajar/app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
View
2 belajar/app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
8 belajar/app/mailers/user_mailer.rb
@@ -0,0 +1,8 @@
+class UserMailer < ActionMailer::Base
+ default from: "from@example.com"
+
+ def registration_confirmation(user)
+ @user = user
+ mail(:to => user.email, :subject => "Registered")
+ end
+end
View
31 belajar/app/models/user.rb
@@ -6,12 +6,35 @@ class User < ActiveRecord::Base
has_many :shared_articles,
:class_name => "Article",
:foreign_key => "user_id",
- :condition =>"title like '%share%'"
-
+ :conditions =>"title like '%share%'"
+ attr_accessor :password
+ before_save :encrypt_password
+
+ validates :password, :presence => {:on => :create},
+ :confirmation => true
+ validates :email, :presence => true, :uniqueness => true
+
+ def encrypt_password
+ if password.present?
+ self.password_salt = BCrypt::Engine.generate_salt
+ self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
+ end
+ end
+
def show_full_name
"#{self.first_name} #{self.last_name}"
end
+
+ def self.authenticate(email, password)
+ user = find_by_email(email)
+ if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
+ user
+ else
+ nil
+ end
+end
+
validates :first_name, :presence => true,
:length => {:minimum => 1, :maximum => 20},
:format => {:with => /[a-zA-Z\s]+$/}
@@ -26,5 +49,7 @@ def show_full_name
validates :username, :presence => true,
:length => {:minimum => 3, :maximum => 254},
:uniqueness => true,
- :format => {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i}
+ :format => {:with => /[a-zA-Z\s]+$/}
+
end
+
View
2 belajar/app/views/articles/edit.html.erb
@@ -23,7 +23,7 @@
Title : <br/><%= f.text_field :title %> <br/>
Article :<br/> <%= f.text_area :body %><br/>
Rating : <%= f.text_field :rating %><br/>
- <%= f.submit "Submit" %>
+ <%= f.submit "Update" %><br/>
<%= link_to "Back to Index",articles_path%>
<% end %>
View
4 belajar/app/views/articles/index.html.erb
@@ -14,12 +14,12 @@
<th>Manage</th>
</tr>
- <% @articles.each do |article| %>
+ <% @article.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.body %></td>
<td><%= article.rating %></td>
- <td><%= link_to "show", article_path(article.id) %> | <%= link_to "edit", edit_article_path(article.id) %>| <%= link_to "delete", article_path(article.id),method: :delete %></td>
+ <td><%= link_to "show", article_path(article.id) %> | <%= link_to "edit", edit_article_path(article.id) %>| <%= link_to "delete", article_path(article.id), confirm:'yakin Hapus ?',method: :delete %></td>
</tr>
<% end %>
View
4 belajar/app/views/articles/new.html.erb
@@ -14,6 +14,10 @@
Title : <br/><%= f.text_field :title %> <br/>
Article :<br/> <%= f.text_area :body %><br/>
Rating : <%= f.text_field :rating %><br/>
+
+ <% user=User.find_by_email(current_user.email) %>
+ <%= f.hidden_field :user_id, :value => user.id %>
+
<%= f.submit "Submit" %>
<% end %>
<%= link_to "Back to Index",articles_path%>
View
14 belajar/app/views/articles/show.html.erb
@@ -3,6 +3,20 @@
Title : <%= @article.title %><br/>
Body : <%= @article.body %><br/>
Rating : <%= @article.rating %><br/>
+ Comment :<br/>
+ <% @comments.each do |comment| %>
+ <br /> <%= comment.content %><br />
+
+ <% end %>
+
+ <% unless (current_user.nil?) %>
+ <%= form_for @comment, :action=>"show", :controller=>"comments" do |f| %>
+ <%= f.text_area :content %>
+ <%= f.hidden_field :article_id, :value => @article.id %>
+ <%= submit_tag "comment" %>
+ <% end %>
+ <% end %>
+
<%= link_to "back",articles_path %>
</body>
</html>
View
4 belajar/app/views/layouts/application.html.erb
@@ -7,6 +7,10 @@
<%= csrf_meta_tags %>
</head>
<body>
+<div id="user_nav">
+ <%= raw(welcome_text) %>
+
+</div>
<%= yield %>
View
13 belajar/app/views/sessions/new.html.erb
@@ -0,0 +1,13 @@
+<h1>Log in</h1>
+ <%= flash[:notice] || flash[:error]%>
+ <%= form_tag sessions_path do %>
+ <p>
+ <%= label_tag :email %><br />
+ <%= text_field_tag :email, params[:email] %>
+ </p>
+ <p>
+ <%= label_tag :password %><br />
+ <%= password_field_tag :password %>
+ </p>
+ <p class="button"><%= submit_tag "Log in"%></p>
+<% end %>
View
5 belajar/app/views/user_mailer/registration_confirmation.html.erb
@@ -0,0 +1,5 @@
+<%= @user.email %>
+
+Thank you for registering !
+
+<p> <%= link_to "Edit Profil", edit_user_url(@user,:host => "localhost:3000" ) %> </p>
View
42 belajar/app/views/users/new.html.erb
@@ -0,0 +1,42 @@
+<h1>Sign Up</h1>
+ <%= flash[:error]%>
+<%= form_for @user do |f| %>
+ <% if @user.errors.any? %>
+ <div class="error_messages">
+ <h2>Form is invalid</h2>
+ <ul>
+
+ <% for message in @user.errors.full_messages %>
+ <li><%= message %></li>
+
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+ <p>
+ <%= f.label :first_name %><br />
+ <%= f.text_field :first_name %>
+ </p>
+ <p>
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name %>
+ </p>
+ <p>
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </p>
+ <p>
+ <%= f.label :username %><br />
+ <%= f.text_field :username %>
+ </p>
+ <p>
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </p>
+ <p>
+ <%= f.label :password_confirmation %> <br/>
+ <%= f.password_field :password_confirmation %>
+ </p>
+ <%= recaptcha_tags %>
+ <p class="button"><%= f.submit %></p>
+<% end %>
View
13 belajar/config/environments/development.rb
@@ -27,4 +27,17 @@
# Expands the lines which load the assets
config.assets.debug = true
+
+ config.action_mailer.raise_delivery_errors = true
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
+ config.action_mailer.smtp_settings = {
+ :address => "smtp.gmail.com",
+ :port => 587,
+ :domain => "executionisqueen.com",
+ :user_name => "training.ror",
+ :password => "karapitan100",
+ :authentication => "plain",
+ :enable_starttls_auto => true
+ }
+
end
View
2 belajar/config/initializers/recaptcha.rb
@@ -0,0 +1,2 @@
+ENV['RECAPTCHA_PUBLIC_KEY'] = '6Lf3HdESAAAAALVS74LZkn-0ogUWJWJdYGxwZlcT'
+ENV['RECAPTCHA_PRIVATE_KEY'] = '6Lf3HdESAAAAAPvYuMtIO92oMsLHGEqI2NdWfOIH'
View
15 belajar/config/routes.rb
@@ -1,4 +1,9 @@
Belajar::Application.routes.draw do
+
+ get "sessions/new"
+
+ get "users/new"
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -55,5 +60,15 @@
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
#match ':controller(/:action(/:id(.:format)))'
+ #get "sign_up" =>
+ get "log_in" => "sessions#new", :as => "log_in"
+ get "log_out" => "sessions#destroy", :as => "log_out"
+
+ get "sign_up" => "users#new", :as => "sign_up"
+ root :to => "users#new"
+
+ resources :users
+ resources :sessions
+ resources :comments
resources :articles
end
View
6 belajar/db/migrate/20120504060927_add_pass_to_user.rb
@@ -0,0 +1,6 @@
+class AddPassToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :password_hash, :string
+ add_column :users, :password_salt, :string
+ end
+end
View
5 belajar/db/migrate/20120506143215_add_userid_toarticle.rb
@@ -0,0 +1,5 @@
+class AddUseridToarticle < ActiveRecord::Migration
+ def change
+ add_column :articles, :user_id, :integer
+ end
+end
View
5 belajar/db/migrate/20120506143242_add_articles_tocomment.rb
@@ -0,0 +1,5 @@
+class AddArticlesTocomment < ActiveRecord::Migration
+ def change
+ add_column :comments, :article_id, :integer
+ end
+end
View
6 belajar/db/schema.rb
@@ -11,14 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120503040257) do
+ActiveRecord::Schema.define(:version => 20120506143242) do
create_table "articles", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "rating"
+ t.integer "user_id"
end
create_table "categories", :force => true do |t|
@@ -38,6 +39,7 @@
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "article_id"
end
create_table "countries", :force => true do |t|
@@ -65,6 +67,8 @@
t.string "address"
t.integer "age"
t.string "birthday"
+ t.string "password_hash"
+ t.string "password_salt"
end
create_table "welcomes", :force => true do |t|
View
7 belajar/test/functional/comments_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
9 belajar/test/functional/sessions_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+end
View
7 belajar/test/functional/user_mailer_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserMailerTest < ActionMailer::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
9 belajar/test/functional/users_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class UsersControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+end
View
4 belajar/test/unit/helpers/comments_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CommentsHelperTest < ActionView::TestCase
+end
View
4 belajar/test/unit/helpers/sessions_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end
View
4 belajar/test/unit/helpers/users_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class UsersHelperTest < ActionView::TestCase
+end

0 comments on commit 6fe0e7e

Please sign in to comment.