Skip to content

Commit

Permalink
added category model and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scottkf committed Oct 18, 2010
1 parent 709812f commit 6db0d8c
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem "mysql"
gem "compass"
gem "devise"
gem "cancan"
gem "ancestry", :git => "git://github.com/scottkf/ancestry.git", :branch => "master"

group :development, :test do
gem "rspec"
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: git://github.com/scottkf/ancestry.git
revision: d8371ebb9d1295dd24155ceca1e13799c76f2149
specs:
ancestry (1.2.1.beta.1)
activerecord (>= 2.1.0)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -144,6 +151,7 @@ PLATFORMS
ruby

DEPENDENCIES
ancestry!
autotest
autotest-rails
cancan
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class CategoryController < ApplicationController
load_and_authorize_resource

layout :article_layout, :only => [:list]

def index
end

def show
end

def create
end

def update
end

def edit
end

def destroy
end

def list
@layout = params[:name]
end

private
def article_layout
@layout
end

end
2 changes: 2 additions & 0 deletions app/helpers/category_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CategoryHelper
end
4 changes: 3 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ def initialize(user)
if user.role? :super_admin
can :see_timestamps, User
can :manage, :all
elsif user.role? :Normal
elsif user.role? :editor
can :read, Article
# manage articles he owns
can :create, Article
can :manage, Article, :user_id => user.id
# can manage his account
can :manage, User, :id => user.id
can :see_timestamps, User, :id => user.id
# can manage, but not delete categories
can [:read, :create, :update], Category
else
can :read, Article
end
Expand Down
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :category

validates :title, :presence => true
validates :body, :presence => true
Expand Down
22 changes: 22 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Category < ActiveRecord::Base

has_ancestry
has_many :articles

validates :name, :presence => true, :uniqueness => true
validates :layout, :presence => true
validates :description, :presence => true
validates :url, :presence => true, :uniqueness => true

before_create :layout_exists

validates_format_of :url, :with => /\A[a-zA-Z0-9]+\z/, :message => "No special characters are allowed"
validates_length_of :url, :in => 3..10, :too_short => "Must have atleast 3 characters", :too_long => "Must have 10 or fewers characters"

private
def layout_exists
if !File.exists?(Rails.root.join("app", "views", "layouts", "#{self.layout}.html.erb"))
File.open(Rails.root.join("app", "views", "layouts", "#{self.layout}.html.erb"), 'w') {|f| f.write("<html>\n<body>\n</body>\n</html>\n") }
end
end
end
18 changes: 13 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ class User < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => true, :email => true


def default_role
self.role_ids = [2] unless self.roles.size > 0
def name
self.first_name + " " + self.last_name
end


def editor_role
self.role_ids = [2]
end

def admin_role
self.role_ids = [1]
end

def name
self.first_name + " " + self.last_name
end

def admin?
self.role? :super_admin
Expand All @@ -37,6 +39,12 @@ def user_admin?
end


def default_role
self.role_ids = [3] unless self.roles.size > 0
end



def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
Expand Down
2 changes: 2 additions & 0 deletions app/views/category/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#create</h1>
<p>Find me in app/views/category/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#destroy</h1>
<p>Find me in app/views/category/destroy.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#edit</h1>
<p>Find me in app/views/category/edit.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#index</h1>
<p>Find me in app/views/category/index.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#list</h1>
<p>Find me in app/views/category/list.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#show</h1>
<p>Find me in app/views/category/show.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/category/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Category#update</h1>
<p>Find me in app/views/category/update.html.erb</p>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

resources :articles, :except => "new"

resources :categories
match "/:name" => "categories#list"


match "/text_styles" => "articles#text_styles"
Expand Down
31 changes: 31 additions & 0 deletions db/migrate/20101018120638_create_categories_and_more.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class CreateCategoriesAndMore < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.string :name
t.string :layout
t.string :description
t.string :url
t.string :ancestry
t.timestamps
end

add_index :categories, :ancestry
add_column :articles, :category_id, :integer

# rename normal role
r = Role.find(2)
r.name = "Editor"
r.save
# add new, regular person role
r = Role.new(:name => "Regular")
r.id = 3
r.save
end

def self.down
drop_table :categories

remove_index :categories, :ancestry
remove_column :articles, :category_id
end
end
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20101011180011) do
ActiveRecord::Schema.define(:version => 20101018120638) do

create_table "articles", :force => true do |t|
t.string "title"
Expand All @@ -21,8 +21,21 @@
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "comments_disabled", :default => false
t.integer "category_id"
end

create_table "categories", :force => true do |t|
t.string "name"
t.string "layout"
t.string "description"
t.string "url"
t.string "ancestry"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "categories", ["ancestry"], :name => "index_categories_on_ancestry"

create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at"
Expand Down
6 changes: 5 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
r.id = 1
r.save

r = Role.new(:name => "Normal")
r = Role.new(:name => "Editor")
r.id = 2
r.save

r = Role.new(:name => "Regular")
r.id = 3
r.save
2 changes: 2 additions & 0 deletions features/article.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Feature: Articles
@javascript @adding
Scenario: Adding an article
Given I am an authenticated user with name "jim" "johnson"
And I am an editor
And I am on the home page
When I fill in "article[title]" with "hello"
And I fill in "article[body]" with "_this is the body_"
Expand All @@ -61,6 +62,7 @@ Feature: Articles
@editing
Scenario: Editing an article
Given I am an authenticated user with name "lifetime" "sack"
And I am an editor
And I have an article
And the article has a title "hello"
And the article has a body "....L"
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/account_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
@user.save
end

Given /^I am an editor$/ do
@user.roles = [Role.find_by_name("Editor")]
@user.save
end
7 changes: 7 additions & 0 deletions features/support/blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@
body {"_here is some content_"}
user
end

Category.blueprint do
name {"hello"}
layout {"filename"}
description {"this is a description"}
url {"valid"}
end
1 change: 1 addition & 0 deletions features/user_management.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Feature: User page
@editing
Scenario: Editing a user
Given I am an authenticated user with name "steve" "brown"
And I am an admin
And I am on the users page
And I follow "Edit"
When I fill in "user[first_name]" with "hello"
Expand Down
Loading

0 comments on commit 6db0d8c

Please sign in to comment.