Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added categories

  • Loading branch information...
commit 40d954190ead34c2080c094882dfb5236766dc0c 1 parent cfacc42
@vaski authored
View
2  app/controllers/ads_controller.rb
@@ -4,7 +4,7 @@ class AdsController < ApplicationController
def index
@ads = @ads.where(state: 'published').paginate(page: params[:page],
per_page: 5,
- include: :images)
+ include: [:images, :categories] )
end
def create
View
2  app/controllers/pages_controller.rb
@@ -2,7 +2,7 @@ class PagesController < ApplicationController
def verified_ads
@ads = Ad.where(state: 'verified').paginate(page: params[:page],
per_page: 5,
- include: :images)
+ include: [:images, :categories])
authorize! :manage, User
end
end
View
2  app/controllers/users_controller.rb
@@ -8,7 +8,7 @@ def index
def show
@ads = @user.ads.paginate(page: params[:page],
per_page: 5,
- include: :images)
+ include: [:images, :categories])
end
def destroy
View
4 app/models/ad.rb
@@ -12,9 +12,11 @@
#
class Ad < ActiveRecord::Base
- attr_accessible :description, :title, :images_attributes
+ attr_accessible :description, :title, :images_attributes, :category_ids
belongs_to :user
has_many :images, dependent: :destroy, autosave: :true
+ has_many :categorizations, dependent: :destroy, autosave: :true
+ has_many :categories, through: :categorizations
accepts_nested_attributes_for :images, allow_destroy: true
validates :title, presence: true,
View
28 app/models/categorization.rb
@@ -0,0 +1,28 @@
+# == Schema Information
+#
+# Table name: categorizations
+#
+# id :integer not null, primary key
+# ad_id :integer
+# category_id :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class Categorization < ActiveRecord::Base
+ attr_accessible :ad_id, :category_id
+ belongs_to :ad
+ belongs_to :category
+
+ validates :ad_id, numericality: true
+ validates :category_id, numericality: true
+
+ after_save do |ctg|
+ ctg.ad.refresh
+ end
+
+ after_destroy do |ctg|
+ ctg.ad.refresh
+ end
+
+end
View
19 app/models/category.rb
@@ -0,0 +1,19 @@
+# == Schema Information
+#
+# Table name: categories
+#
+# id :integer not null, primary key
+# category_name :string(255)
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class Category < ActiveRecord::Base
+ attr_accessible :category_name
+ has_many :categorizations, dependent: :restrict
+ has_many :ads, through: :categorizations
+
+ validates :category_name, presence: true,
+ length: { maximum: 80 }
+
+end
View
5 app/views/ads/_ad.html.erb
@@ -7,6 +7,11 @@
</div>
<div class="span10">
<h2><%= link_to ad.title, ad_path(ad) %></h2>
+ <p> Categories:
+ <% ad.categories.each do |category| %>
+ <span class="label label-info"><%= category.category_name %></span>
+ <% end %>
+ </p>
<p><%= ad.description %></p>
<p><span class="label"><%= ad.state %></span> Updated <%= time_ago_in_words(ad.updated_at) %> ago</p>
<% if can? :update, ad %>
View
1  app/views/ads/_form.html.erb
@@ -6,6 +6,7 @@
<fieldset>
<%= f.input :title, required: false, autofocus: true, input_html: { class: "span6" } %>
<%= f.input :description, required: false, input_html: { class: "span6", rows: 5 } %>
+ <%= f.collection_check_boxes :category_ids, Category.all, :id, :category_name %>
<%= f.simple_fields_for :images do |i| %>
<%= i.input :image_url, required: false, input_html: { class: "span6" } %>
View
5 app/views/ads/show.html.erb
@@ -2,6 +2,11 @@
<div class="row">
<div class="span12">
<h1><%= @ad.title %></h1>
+ <p> Categories:
+ <% @ad.categories.each do |category| %>
+ <span class="label label-info"><%= category.category_name %></span>
+ <% end %>
+ </p>
<div id="images">
<% @ad.images.each do |img| %>
<%= image_tag(img.image_url, class: 'center') %>
View
9 db/migrate/20120918184634_create_categories.rb
@@ -0,0 +1,9 @@
+class CreateCategories < ActiveRecord::Migration
+ def change
+ create_table :categories do |t|
+ t.string :category_name
+
+ t.timestamps
+ end
+ end
+end
View
10 db/migrate/20120918185006_create_categorizations.rb
@@ -0,0 +1,10 @@
+class CreateCategorizations < ActiveRecord::Migration
+ def change
+ create_table :categorizations do |t|
+ t.integer :ad_id
+ t.integer :category_id
+
+ t.timestamps
+ end
+ end
+end
View
15 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120914045858) do
+ActiveRecord::Schema.define(:version => 20120918185006) do
create_table "ads", :force => true do |t|
t.string "title"
@@ -24,6 +24,19 @@
add_index "ads", ["user_id", "created_at"], :name => "index_ads_on_user_id_and_created_at"
+ create_table "categories", :force => true do |t|
+ t.string "category_name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "categorizations", :force => true do |t|
+ t.integer "ad_id"
+ t.integer "category_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "images", :force => true do |t|
t.string "image_url"
t.integer "ad_id"
View
5 spec/models/ad_spec.rb
@@ -24,8 +24,12 @@
it { should respond_to(:state) }
it { should respond_to(:user) }
it { should respond_to(:images) }
+ it { should respond_to(:categories) }
+ it { should_not respond_to(:categorization) }
it { should belong_to(:user) }
it { should have_many(:images) }
+ it { should have_many(:categories) }
+ it { should have_many(:categorizations) }
it { should_not allow_mass_assignment_of(:id) }
it { should_not allow_mass_assignment_of(:user_id) }
it { should_not allow_mass_assignment_of(:created_at) }
@@ -34,6 +38,7 @@
it { should allow_mass_assignment_of(:title) }
it { should allow_mass_assignment_of(:description) }
it { should allow_mass_assignment_of(:images_attributes) }
+ it { should allow_mass_assignment_of(:category_ids) }
it { should validate_presence_of(:title) }
it { should_not allow_value('a' * 81).for(:title) }
it { should_not allow_value('').for(:title) }
View
32 spec/models/categorization_spec.rb
@@ -0,0 +1,32 @@
+# == Schema Information
+#
+# Table name: categorizations
+#
+# id :integer not null, primary key
+# ad_id :integer
+# category_id :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+require 'spec_helper'
+
+describe Categorization do
+
+ it { should respond_to(:id) }
+ it { should respond_to(:ad_id) }
+ it { should respond_to(:category_id) }
+ it { should respond_to(:created_at) }
+ it { should respond_to(:updated_at) }
+ it { should_not respond_to(:ads) }
+ it { should_not respond_to(:categories) }
+ it { should belong_to(:ad) }
+ it { should belong_to(:category) }
+ it { should_not allow_mass_assignment_of(:id) }
+ it { should_not allow_mass_assignment_of(:created_at) }
+ it { should_not allow_mass_assignment_of(:updated_at) }
+ it { should allow_mass_assignment_of(:ad_id) }
+ it { should allow_mass_assignment_of(:category_id) }
+ it { should validate_numericality_of(:ad_id) }
+ it { should validate_numericality_of(:category_id) }
+end
View
32 spec/models/category_spec.rb
@@ -0,0 +1,32 @@
+# == Schema Information
+#
+# Table name: categories
+#
+# id :integer not null, primary key
+# category_name :string(255)
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+require 'spec_helper'
+
+describe Category do
+
+ it { should respond_to(:id) }
+ it { should respond_to(:category_name) }
+ it { should respond_to(:created_at) }
+ it { should respond_to(:updated_at) }
+ it { should respond_to(:ads) }
+ it { should_not respond_to(:categorization) }
+ it { should have_many(:ads) }
+ it { should have_many(:categorizations) }
+ it { should_not allow_mass_assignment_of(:id) }
+ it { should_not allow_mass_assignment_of(:created_at) }
+ it { should_not allow_mass_assignment_of(:updated_at) }
+ it { should allow_mass_assignment_of(:category_name) }
+ it { should validate_presence_of(:category_name) }
+ 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) }
+end
Please sign in to comment.
Something went wrong with that request. Please try again.