Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring in controllers

  • Loading branch information...
commit 07b534fa951703efffdba2164a9affa45cd0903b 1 parent b624dcb
@vaski authored
View
1  Gemfile
@@ -11,6 +11,7 @@ gem 'will_paginate', '~> 3.0'
gem 'bootstrap-will_paginate'
gem 'simple_form'
gem 'nested_form'
+gem 'enumerize'
group :development, :test do
gem 'rspec-rails', '~> 2.0'
View
3  Gemfile.lock
@@ -60,6 +60,8 @@ GEM
railties (~> 3.1)
warden (~> 1.2.1)
diff-lcs (1.1.3)
+ enumerize (0.4.0)
+ activesupport (>= 3.2)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
@@ -176,6 +178,7 @@ DEPENDENCIES
capybara (~> 1.1.2)
coffee-rails (~> 3.2.1)
devise
+ enumerize
factory_girl_rails
faker
jquery-rails
View
28 app/controllers/ads_controller.rb
@@ -1,21 +1,17 @@
class AdsController < ApplicationController
load_and_authorize_resource
+ respond_to :html
def index
- @ads = @ads.where(state: 'published').paginate(page: params[:page],
- per_page: 5,
- include: [:images, :categories] )
+ @ads = @ads.published.paginate(page: params[:page],
+ per_page: 5,
+ include: [:images, :categories] )
end
def create
@ad = current_user.ads.build(params[:ad])
- if @ad.save
- flash[:success] = "Ad created!"
- redirect_to @ad
- else
- flash.now[:error] = "Ad not created!"
- render 'new'
- end
+ flash[:success] = 'Ad created!' if @ad.save
+ respond_with @ad
end
def new
@@ -23,19 +19,13 @@ def new
end
def update
- if @ad.update_attributes(params[:ad])
- flash[:success] = "Ad updated!"
- redirect_to ad_path
- else
- flash.now[:error] = "Ad not updated!"
- render 'edit'
- end
+ flash[:success] = 'Ad updated!' if @ad.update_attributes(params[:ad])
+ respond_with @ad
end
def destroy
@ad.destroy
- flash[:notice] = "Successfuly destroyed ad!"
- redirect_to current_user
+ redirect_to current_user, notice: 'Successfuly destroyed ad!'
end
def verify
View
14 app/controllers/categories_controller.rb
@@ -2,32 +2,28 @@ class CategoriesController < ApplicationController
load_and_authorize_resource
def index
- @categories = @categories.paginate(page: params[:page], include: :categorizations)
+ @categories = @categories.paginate(page: params[:page],
+ include: :categorizations)
end
def create
if @category.save
- flash[:success] = "Category created!"
- redirect_to categories_path
+ redirect_to categories_path, notice: 'Category created!'
else
- flash.now[:error] = "Category not created!"
render 'new'
end
end
def update
if @category.update_attributes(params[:category])
- flash[:success] = "Category updated!"
- redirect_to categories_path
+ redirect_to categories_path, notice: 'Category updated!'
else
- flash.now[:error] = "Category not updated!"
render 'edit'
end
end
def destroy
@category.destroy
- flash[:success] = "Category destroyed!"
- redirect_to categories_path
+ redirect_to categories_path, notice: 'Category destroyed!'
end
end
View
6 app/controllers/pages_controller.rb
@@ -1,8 +1,8 @@
class PagesController < ApplicationController
def verified_ads
- @ads = Ad.where(state: 'verified').paginate(page: params[:page],
- per_page: 5,
- include: [:images, :categories])
+ @ads = Ad.verified.paginate(page: params[:page],
+ per_page: 5,
+ include: [:images, :categories])
authorize! :create, Category
end
end
View
34 app/controllers/users_controller.rb
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
- load_and_authorize_resource except: :create
- skip_authorize_resource only: :index
+ load_and_authorize_resource
+ respond_to :html
def index
@users = @users.paginate(page: params[:page])
@@ -14,35 +14,19 @@ def show
end
def create
- role_param = params[:user][:role]
- params[:user].delete(:role)
- @user = User.new(params[:user])
- authorize! :create, @user
- @user.role = role_param if can? :assign_role, @user
- if @user.save
- flash[:success] = "User created!"
- redirect_to users_path
- else
- flash.now[:error] = "User not created!"
- render 'new'
- end
+ @user.role = params[:user][:role] if can? :assign_role, @user
+ flash[:success] = 'User created!' if @user.save
+ respond_with @user
end
def update
- role_param = params[:user][:role]
- @user.role = role_param if can? :assign_role, @user
- if @user.update_attributes(params[@user])
- flash[:success] = "User updated!"
- redirect_to users_path
- else
- flash.now[:error] = "User not updated!"
- render 'edit'
- end
+ @user.role = params[:user][:role] if can? :assign_role, @user
+ flash[:success] = 'User updated!' if @user.save
+ respond_with @user
end
def destroy
@user.destroy
- flash[:success] = "User destroyed!"
- redirect_to users_path
+ redirect_to users_path, notice: 'User destroyed!'
end
end
View
8 app/models/ability.rb
@@ -5,10 +5,10 @@ def initialize(user)
user ||= User.new # guest user (not logged in)
- if user.role == 'admin'
+ if user.role.admin?
can :read, :all
- can [:create, :update, :assign_role], User
- can :destroy, User do |usr|
+ can :create, User
+ can [:destroy, :update, :assign_role], User do |usr|
user.id != usr.id
end
can [:destroy, :approve, :reject], Ad
@@ -16,7 +16,7 @@ def initialize(user)
else
can :read, Ad, state: 'published'
- if user.role == 'user'
+ if user.role.user?
can :read, User, id: user.id
can :create, Ad
can [:read, :update, :destroy, :verify], Ad, user_id: user.id
View
3  app/models/ad.rb
@@ -29,6 +29,9 @@ class Ad < ActiveRecord::Base
default_scope order: 'ads.updated_at DESC'
+ scope :published, -> { where(state: 'published') }
+ scope :verified, -> { where(state: 'verified') }
+
before_save do |ad|
ad.state = 'draft' if ad.title_changed? || ad.description_changed?
end
View
3  app/models/user.rb
@@ -20,6 +20,7 @@
#
class User < ActiveRecord::Base
+ include Enumerize
before_create :set_role
# Include default devise modules. Others available are:
@@ -39,6 +40,8 @@ class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, format: { with: VALID_EMAIL_REGEX }
+ enumerize :role, in: [:user, :admin], default: :user
+
private
def set_role
View
4 app/views/layouts/_navbar.html.erb
@@ -4,7 +4,7 @@
<%= link_to "ADS project", root_path, class: "brand" %>
<nav>
<ul class="nav pull-right">
- <% if user_signed_in? %>
+ <% if user_signed_in? %>
<% if can? :create, User %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Categories", categories_path %></li>
@@ -21,7 +21,7 @@
<li><%= link_to "Sign out", destroy_user_session_path, method: :delete %></li>
</ul>
</li>
- <% else %>
+ <% else %>
<li><%= link_to "Sign in", new_user_session_path %></li>
<% end %>
</ul>
View
8 app/views/users/_user.html.erb
@@ -2,19 +2,13 @@
<td>
<p>
<%= link_to user.name, user %>
- <% if user.role == 'admin' %>
- <span class="label label-important"><%= user.role %></span>
- <% else %>
- <span class="label label-success"><%= user.role %></span>
- <% end %>
+ <span class="label label-<%= (user.role == 'admin') ? 'important' : 'success' %>"><%= user.role %></span>
</p>
</td>
<td>
<p class="right">
<% if (can? :destroy, User) && (user != current_user) %>
<%= link_to "Delete", user, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-mini btn-danger' %>
- <% end %>
- <% if can? :update, User %>
<%= link_to "Change role", edit_user_path(user), class: 'btn btn-mini btn-primary' %>
<% end %>
</p>
View
4 app/views/users/edit.html.erb
@@ -21,9 +21,7 @@
</table>
<%= simple_form_for @user do |f| %>
- <div class="form-inputs">
- <%= f.input :role, required: false, input_html: { class: 'span6' } %>
- </div>
+ <%= f.input :role, as: :radio_buttons, required: false, input_html: { class: 'span6' } %>
<%= f.button :submit, class: 'btn-primary' %>
<%= link_to 'Cancel', users_path, class: 'btn' %>
View
12 app/views/users/new.html.erb
@@ -5,13 +5,11 @@
<div class="span6 offset3">
<%= simple_form_for @user do |f| %>
- <div class="form-inputs">
- <%= f.input :name, required: false, autofocus: true, input_html: { class: 'span6' } %>
- <%= f.input :email, required: false, autofocus: true, input_html: { class: 'span6' } %>
- <%= f.input :password, required: false, input_html: { class: 'span6' } %>
- <%= f.input :password_confirmation, required: false, input_html: { class: 'span6' } %>
- <%= f.input :role, required: false, input_html: { class: 'span6' } %>
- </div>
+ <%= f.input :name, required: false, autofocus: true, input_html: { class: 'span6' } %>
+ <%= f.input :email, required: false, autofocus: true, input_html: { class: 'span6' } %>
+ <%= f.input :password, required: false, input_html: { class: 'span6' } %>
+ <%= f.input :password_confirmation, required: false, input_html: { class: 'span6' } %>
+ <%= f.input :role, as: :radio_buttons, required: false, input_html: { class: 'span6' } %>
<%= f.button :submit, "Sign up", class: 'btn-primary' %>
<%= link_to 'Cancel', users_path, class: 'btn' %>
View
2  config/environments/development.rb
@@ -23,7 +23,7 @@
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
- config.active_record.mass_assignment_sanitizer = :strict
+ # config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
View
2  config/environments/test.rb
@@ -30,7 +30,7 @@
config.action_mailer.delivery_method = :test
# Raise exception on mass assignment protection for Active Record models
- config.active_record.mass_assignment_sanitizer = :strict
+ # config.active_record.mass_assignment_sanitizer = :strict
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
View
5 spec/requests/ad_pages_spec.rb
@@ -13,11 +13,6 @@
it "should not create an ad" do
expect { click_button "Save" }.not_to change(Ad, :count)
end
-
- describe "should have error message" do
- before { click_button "Save" }
- it { should have_content('Ad not created!') }
- end
end
describe "with valid information" do
View
1  spec/requests/user_pages_spec.rb
@@ -91,7 +91,6 @@
it { should have_selector('title', text: 'All users') }
it { should have_selector('h1', text: 'All users') }
- it { should have_selector('a', text: 'Change role') }
it { should have_link('Users', href: users_path) }
describe "deleting user" do
Please sign in to comment.
Something went wrong with that request. Please try again.