Browse files

task 6

  • Loading branch information...
1 parent 8d92796 commit 70d121cd5013b970a08638435f325f78486ec7a7 @varid007 committed May 8, 2012
Showing with 301 additions and 26 deletions.
  1. +1 −0 belajar/Gemfile
  2. +3 −0 belajar/Gemfile.lock
  3. +3 −0 belajar/app/assets/javascripts/admin/articles.js.coffee
  4. +3 −0 belajar/app/assets/javascripts/admin/comments.js.coffee
  5. +3 −0 belajar/app/assets/stylesheets/admin/articles.css.scss
  6. +3 −0 belajar/app/assets/stylesheets/admin/comments.css.scss
  7. +28 −0 belajar/app/controllers/admin/application_controller.rb
  8. +60 −0 belajar/app/controllers/admin/articles_controller.rb
  9. +15 −0 belajar/app/controllers/admin/comments_controller.rb
  10. +6 −1 belajar/app/controllers/application_controller.rb
  11. +8 −8 belajar/app/controllers/articles_controller.rb
  12. +11 −9 belajar/app/controllers/comments_controller.rb
  13. +2 −0 belajar/app/helpers/admin/articles_helper.rb
  14. +2 −0 belajar/app/helpers/admin/comments_helper.rb
  15. +32 −0 belajar/app/views/admin/articles/edit.html.erb
  16. +29 −0 belajar/app/views/admin/articles/index.html.erb
  17. +17 −0 belajar/app/views/admin/articles/new.html.erb
  18. +22 −0 belajar/app/views/admin/articles/show.html.erb
  19. +3 −0 belajar/app/views/articles/_comments.html.erb
  20. +1 −1 belajar/app/views/articles/index.html.erb
  21. +5 −5 belajar/app/views/articles/show.html.erb
  22. +3 −0 belajar/app/views/comments/create.js.erb
  23. +3 −0 belajar/app/views/layouts/application.html.erb
  24. +4 −1 belajar/config/environments/development.rb
  25. +4 −0 belajar/config/routes.rb
  26. +6 −0 belajar/db/migrate/20120507151906_add_isadmin_touser.rb
  27. +2 −1 belajar/db/schema.rb
  28. +7 −0 belajar/test/functional/admin/articles_controller_test.rb
  29. +7 −0 belajar/test/functional/admin/comments_controller_test.rb
  30. +4 −0 belajar/test/unit/helpers/admin/articles_helper_test.rb
  31. +4 −0 belajar/test/unit/helpers/admin/comments_helper_test.rb
View
1 belajar/Gemfile
@@ -10,6 +10,7 @@ gem 'execjs'
gem 'therubyracer'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'recaptcha', :require => 'recaptcha/rails'
+gem 'exception_notification'
# Gems used only for assets and not required
# in production environments by default.
View
3 belajar/Gemfile.lock
@@ -42,6 +42,8 @@ GEM
execjs
coffee-script-source (1.3.1)
erubis (2.7.0)
+ exception_notification (2.6.1)
+ actionmailer (>= 3.0.4)
execjs (1.3.1)
multi_json (~> 1.0)
hike (1.2.1)
@@ -117,6 +119,7 @@ PLATFORMS
DEPENDENCIES
bcrypt-ruby
coffee-rails (~> 3.1.0)
+ exception_notification
execjs
jquery-rails
mysql2
View
3 belajar/app/assets/javascripts/admin/articles.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/admin/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/stylesheets/admin/articles.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the admin/articles 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/admin/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the admin/comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
28 belajar/app/controllers/admin/application_controller.rb
@@ -0,0 +1,28 @@
+class Admin::ApplicationController < ApplicationController
+ protect_from_forgery
+
+ helper_method :current_user
+
+ def require_admin_login
+ if current_user.nil? || !is_admin
+ flash[:error] = "Only admins are permitted"
+ redirect_to articles_path
+ else
+ return current_user
+ end
+ end
+
+ private
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if
+ session[:user_id]
+ end
+
+ def is_admin
+ if current_user.email == "sadewa44@gmail.com"
+ true
+ else
+ false
+ end
+ end
+end
View
60 belajar/app/controllers/admin/articles_controller.rb
@@ -0,0 +1,60 @@
+class Admin::ArticlesController < Admin::ApplicationController
+
+ before_filter :require_admin_login, :only => [:index,:new, :create, :edit, :update, :destroy]
+ before_filter :find_article,:only => [:show,:edit, :delete, :destroy]
+ def index
+ @articles = Article.all
+ end
+
+ def create
+ @article=Article.new(params[:article])
+
+ if @article.save
+ flash[:notice] = 'Article was succesful create'
+ redirect_to admin_articles_path
+ else
+ flash[:error] = 'Article was Error'
+ render new_admin_articles_path
+ end
+ end
+
+ def new
+ @article = Article.new
+
+ end
+
+ def edit
+
+ end
+
+ def show
+ @comments=@article.comments
+ @comment=@article.comments.new
+ end
+
+ def update
+
+ if @article.update_attributes(params[:article])
+ flash[:notice] = 'Article was succesful Update'
+ redirect_to admin_articles_path
+ else
+ flash[:error] = "Article was failed to update"
+ render :edit
+ end
+
+ end
+
+ def destroy
+ @article.destroy
+ redirect_to admin_articles_path
+ end
+
+ def find_article
+ @article = Article.find_by_id(params[:id])
+ if @article.nil?
+ flash[:notice] = 'Article not found'
+ redirect_to admin_articles_path
+ end
+ end
+
+end
View
15 belajar/app/controllers/admin/comments_controller.rb
@@ -0,0 +1,15 @@
+class Admin::CommentsController < ApplicationController
+
+ def create
+ @comment = Comment.new(params[:comment])
+ @article = Article.find_by_id(params[:comment][:article_id])
+ @comments=@article.comment
+ if @comment.save
+ flash[:notice]= "Comment posted"
+ #di bawah direct di ganti
+ redirect_to article_path(@article)
+ end
+
+
+ end
+end
View
7 belajar/app/controllers/application_controller.rb
@@ -17,5 +17,10 @@ def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
-
+ rescue_from NoMethodError, :with => :record_not_found
+
+
+ def record_not_found
+ render "/public/500.html", :status => 500, :layout => false
+ end
end
View
16 belajar/app/controllers/articles_controller.rb
@@ -1,11 +1,11 @@
class ArticlesController < ApplicationController
- before_filter :require_login, :only => [:new ,:edit, :delete, :update,:destroy]
+
+ before_filter :require_login, :only => [:new,:edit, :delete, :update,:destroy]
before_filter :find_article, :only => [:show, :edit, :delete, :update,:destroy]
before_filter :your_article, :only => [:edit, :delete, :update,:destroy]
-
def index
- @article = Article.all
+ @articles = Article.all
end
def create
@@ -23,6 +23,7 @@ def create
def new
@article = Article.new
+
end
@@ -36,33 +37,32 @@ def show
end
def update
-
if @article.update_attributes(params[:article])
flash[:notice] = 'Article was succesful Update'
redirect_to articles_path
else
render :edit
end
-
end
def destroy
@article.destroy
redirect_to articles_path
end
+ private
def find_article
@article = Article.find_by_id(params[:id])
if @article.nil?
- flash[:notice] = 'Error Detected'
- redirect_to :controller => :articles, :action =>"new"
+ flash[:notice] = 'Article not found'
+ redirect_to articles_path
end
end
def your_article
user=User.find_by_email(current_user.email)
- unless @article.user_id == user.id
+ unless @article.user_id == user.id || is_admin
flash[:notice]="its not your article"
redirect_to articles_path
View
20 belajar/app/controllers/comments_controller.rb
@@ -1,16 +1,18 @@
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])
+ # @article = Article.find_by_id(params[:comment][:article_id])
+ # @comments=@article.comment
+ #if @comment.save
+ # flash[:notice]= "Comment posted"
+ #di bawah direct di ganti
+ # redirect_to article_path(@article)
+ # end
if @comment.save
- flash[:notice]= "Comment posted"
- redirect_to articles_path
+ format.html { redirect_to(article_path(article), :notice => 'Comment was successfully created.') }
+ format.js
end
-
+
end
end
View
2 belajar/app/helpers/admin/articles_helper.rb
@@ -0,0 +1,2 @@
+module Admin::ArticlesHelper
+end
View
2 belajar/app/helpers/admin/comments_helper.rb
@@ -0,0 +1,2 @@
+module Admin::CommentsHelper
+end
View
32 belajar/app/views/admin/articles/edit.html.erb
@@ -0,0 +1,32 @@
+<html>
+<head>
+ <title>Ruby On Rails</title>
+</head>
+
+<body>
+<%= form_for @article,:url =>admin_article_path do |f| %>
+
+ <% if @article.errors.any? %>
+
+ <h2>
+ <%= pluralize(@article.errors.count, "error") %> title or body was Error:
+ </h2>
+
+ <% @article.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+
+<% end %>
+
+
+ Title : <br/><%= f.text_field :title %> <br/>
+ Article :<br/> <%= f.text_area :body %><br/>
+ Rating : <%= f.text_field :rating %><br/>
+ <%= f.submit "Update" %><br/>
+ <%= link_to "Back to Index",admin_articles_path%>
+ <% end %>
+
+</body>
+
+</html>
View
29 belajar/app/views/admin/articles/index.html.erb
@@ -0,0 +1,29 @@
+<html>
+<head>
+ <title>Ruby On Rails</title>
+</head>
+
+<body>
+ <h3><%= flash[:notice]%></h3>
+<%= link_to "Add Article",new_admin_article_path %>
+ <table>
+ <tr>
+ <th>Title</th>
+ <th>Body</th>
+ <th>Rating</th>
+ <th>Manage</th>
+ </tr>
+
+ <% @articles.each do |article| %>
+ <tr>
+ <td><%= article.title %></td>
+ <td><%= article.body %></td>
+ <td><%= article.rating %></td>
+ <td><%= link_to "show", admin_article_path(article.id) %> | <%= link_to "edit", edit_admin_article_path(article.id) %>| <%= link_to "delete", admin_article_path(article.id), confirm:'yakin Hapus ?',method: :delete %></td>
+ </tr>
+ <% end %>
+
+ </table>
+</body>
+
+</html>
View
17 belajar/app/views/admin/articles/new.html.erb
@@ -0,0 +1,17 @@
+
+<%= form_for @article, :url =>admin_articles_path do |f| %>
+ <% if @article.errors.any? %>
+ <%= pluralize(@article.errors.count, "Error") %>
+ <% @article.errors.full_messages.each do |msg| %>
+ <%= msg%>
+ <% end %>
+ <% end %>
+
+ <%= f.label :title, "Title"%></br><%= f.text_field :title %></br>
+ <%= f.label :body, "Article"%></br><%= f.text_area :body %></br>
+ <%= f.label :rating, "Rating"%><%= f.text_field :rating %></br>
+ <% user=User.find_by_email(current_user.email) %>
+ <%= f.hidden_field :user_id, :value => user.id %>
+ <%= f.submit "Save" %>
+<% end %>
+<%= link_to "back" ,admin_articles_path %>
View
22 belajar/app/views/admin/articles/show.html.erb
@@ -0,0 +1,22 @@
+<html>
+<body>
+ Title : <%= @article.title %><br/>
+ Body : <%= @article.body %><br/>
+ Rating : <%= @article.rating %><br/>
+ Comment :<br/>
+
+ <% @comments.each do |comment|%><br/>
+ <%= comment.content %>
+ <% 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",admin_articles_path %>
+</body>
+</html>
View
3 belajar/app/views/articles/_comments.html.erb
@@ -0,0 +1,3 @@
+ <% @comments.each do |comment| %>
+ <br /> <%= comment.content %><br />
+ <% end %>
View
2 belajar/app/views/articles/index.html.erb
@@ -14,7 +14,7 @@
<th>Manage</th>
</tr>
- <% @article.each do |article| %>
+ <% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.body %></td>
View
10 belajar/app/views/articles/show.html.erb
@@ -4,13 +4,13 @@
Body : <%= @article.body %><br/>
Rating : <%= @article.rating %><br/>
Comment :<br/>
- <% @comments.each do |comment| %>
- <br /> <%= comment.content %><br />
-
- <% end %>
+
+ <div id="comments">
+ <%= render :partial => "comments", :locals => {:comments => @comments} %>
+ </div><br /><br />
<% unless (current_user.nil?) %>
- <%= form_for @comment, :action=>"show", :controller=>"comments" do |f| %>
+ <%= form_for @comment, :url => comments_path, :remote => true do |f| %>
<%= f.text_area :content %>
<%= f.hidden_field :article_id, :value => @article.id %>
<%= submit_tag "comment" %>
View
3 belajar/app/views/comments/create.js.erb
@@ -0,0 +1,3 @@
+$("#comments").html(" <%= j( render :partial => "articles/comments",
+ :locals => {:comments => @comments} ) %>")
+$("#comment_content").val("");
View
3 belajar/app/views/layouts/application.html.erb
@@ -2,6 +2,9 @@
<html>
<head>
<title>Belajar</title>
+ <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
+ <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" %>
+ <%= javascript_include_tag "jquery-rails.js" %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
View
5 belajar/config/environments/development.rb
@@ -39,5 +39,8 @@
:authentication => "plain",
:enable_starttls_auto => true
}
-
+ config.middleware.use ExceptionNotifier,
+ :email_prefix => "[Error detected]",
+ :sender_address => %{"notifier" <notifier@example.com>},
+ :exception_recipients => %w{variestie@gmail.com}
end
View
4 belajar/config/routes.rb
@@ -61,6 +61,10 @@
# Note: This route will make all actions in every controller accessible via GET requests.
#match ':controller(/:action(/:id(.:format)))'
#get "sign_up" =>
+ namespace :admin do
+ resources :articles
+ end
+
get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
View
6 belajar/db/migrate/20120507151906_add_isadmin_touser.rb
@@ -0,0 +1,6 @@
+class AddIsadminTouser < ActiveRecord::Migration
+ def change
+ add_column :users, :is_admin, :integer
+ end
+
+end
View
3 belajar/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120506143242) do
+ActiveRecord::Schema.define(:version => 20120507151906) do
create_table "articles", :force => true do |t|
t.string "title"
@@ -69,6 +69,7 @@
t.string "birthday"
t.string "password_hash"
t.string "password_salt"
+ t.integer "is_admin"
end
create_table "welcomes", :force => true do |t|
View
7 belajar/test/functional/admin/articles_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class Admin::ArticlesControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
7 belajar/test/functional/admin/comments_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class Admin::CommentsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 belajar/test/unit/helpers/admin/articles_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class Admin::ArticlesHelperTest < ActionView::TestCase
+end
View
4 belajar/test/unit/helpers/admin/comments_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class Admin::CommentsHelperTest < ActionView::TestCase
+end

0 comments on commit 70d121c

Please sign in to comment.