Permalink
Browse files

Finish with manage categories

  • Loading branch information...
1 parent 40d9541 commit 62e696b68c7067fa7d408950fd237fa8adbd12c3 @vaski committed Sep 19, 2012
@@ -78,3 +78,7 @@ pre {
#images {
margin-bottom: 20px;
}
+
+.right {
+ text-align: right;
+}
@@ -0,0 +1,33 @@
+class CategoriesController < ApplicationController
+ load_and_authorize_resource
+
+ def index
+ @categories = @categories.paginate(page: params[:page], include: :categorizations)
+ end
+
+ def create
+ if @category.save
+ flash[:success] = "Category created!"
+ redirect_to categories_path
+ 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
+ else
+ flash.now[:error] = "Category not updated!"
+ render 'edit'
+ end
+ end
+
+ def destroy
+ @category.destroy
+ flash[:success] = "Category destroyed!"
+ redirect_to categories_path
+ end
+end
@@ -8,15 +8,14 @@ def initialize(user)
if user.role == 'admin'
can :read, :all
can :manage, User
+ can :manage, Category
can [:destroy, :approve, :reject], Ad
else
can :read, Ad
- can :read, Image
if user.role == 'user'
can :read, User, id: user.id
can :create, Ad
- can :mamage, Image
can [:update, :destroy, :verify], Ad, user_id: user.id
end
end
@@ -20,9 +20,4 @@ class Categorization < ActiveRecord::Base
after_save do |ctg|
ctg.ad.refresh
end
-
- after_destroy do |ctg|
- ctg.ad.refresh
- end
-
end
@@ -14,6 +14,7 @@ class Category < ActiveRecord::Base
has_many :ads, through: :categorizations
validates :category_name, presence: true,
- length: { maximum: 80 }
+ length: { maximum: 80 },
+ uniqueness: true
end
@@ -0,0 +1,13 @@
+ <tr>
+ <td><%= category.category_name %></td>
+ <td>
+ <p class="right">
+ <% if ( !category.categorizations.any? ) && (can? :delete, category) %>
+ <%= link_to "Delete", category, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-mini btn-danger' %>
+ <% end %>
+ <% if can? :update, category %>
+ <%= link_to "edit", edit_category_path(category), class: 'btn btn-mini btn-primary' %>
+ <% end %>
+ </p>
+ </td>
+ </tr>
@@ -0,0 +1,14 @@
+<% provide(:title, controller.action_name.humanize + " category") %>
+ <div class="row">
+ <div class="span6 offset3">
+ <h1><%= controller.action_name.humanize + " category" %></h1>
+ <%= simple_nested_form_for @category do |f| %>
+ <fieldset>
+ <%= f.input :category_name, required: false, autofocus: true, input_html: { class: "span6" } %>
+
+ <%= f.button :submit, "Save", class: "btn-primary" %>
+ <%= link_to "Cancel", categories_path, class: "btn" %>
+ </fieldset>
+ <% end %>
+ </div>
+ </div>
@@ -0,0 +1 @@
+<%= render partial: 'form' %>
@@ -0,0 +1,17 @@
+<% provide(:title, 'All categories') %>
+ <h1>All categories</h1>
+ <div class="row">
+ <div class="span6 offset3">
+ <% if can? :create, Category %>
+ <p class="center">
+ <%= link_to "Create category!", new_category_path, class: "btn btn-primary" %>
+ </p>
+ <% end %>
+ <table class="table table-striped">
+ <% @categories.each do |category| %>
+<%= render category %>
+ <% end %>
+ </table>
+ <%= will_paginate %>
+ </div>
+ </div>
@@ -0,0 +1 @@
+<%= render partial: 'form' %>
@@ -7,6 +7,7 @@
<% if user_signed_in? %>
<% if can? :manage, User %>
<li><%= link_to "Users", users_path %></li>
+ <li><%= link_to "Categories", categories_path %></li>
<li><%= link_to "Verified ads", verified_ads_path %></li>
<% end %>
<% if can? :create, Ad %>
View
@@ -4,6 +4,7 @@
resources :users, only: [:show, :index, :destroy]
resources :ads
+ resources :categories, except: :show
get '/ads/:id/verify', to: 'ads#verify', as: 'verify'
get '/ads/:id/approve', to: 'ads#approve', as: 'approve'
@@ -0,0 +1,5 @@
+class AddIndexToCategoryName < ActiveRecord::Migration
+ def change
+ add_index :categories, :category_name, unique: true
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120918185006) do
+ActiveRecord::Schema.define(:version => 20120919053433) do
create_table "ads", :force => true do |t|
t.string "title"
@@ -30,6 +30,8 @@
t.datetime "updated_at", :null => false
end
+ add_index "categories", ["category_name"], :name => "index_categories_on_category_name", :unique => true
+
create_table "categorizations", :force => true do |t|
t.integer "ad_id"
t.integer "category_id"
@@ -24,7 +24,9 @@ namespace :db do
images = ['01','02','03','04','05','06','07','08','09','10']
ads = Ad.all
+ category = Category.create!(category_name: 'Cows')
ads.each do |ad|
+ ad.categorizations.create!(category_id: category.id)
3.times do
image = 'cow' + images.sample + '.png'
ad.images.create!(image_url: image)
View
@@ -25,4 +25,8 @@
image_url "cow01.png"
ad
end
+
+ factory :category do
+ category_name "Cows"
+ end
end
@@ -28,5 +28,14 @@
it { should_not allow_value('a' * 81).for(:category_name) }
it { should_not allow_value('').for(:category_name) }
it { should_not allow_value(nil).for(:category_name) }
- it { should allow_value('toys and games').for(:category_name) }
+ it { should allow_value('Cows').for(:category_name) }
+
+ describe "when category_name is alredy taken" do
+ let(:category) { FactoryGirl.build(:category) }
+ before do
+ same_category = category.dup
+ end
+
+ it { should_not be_valid }
+ end
end

0 comments on commit 62e696b

Please sign in to comment.