Skip to content
Browse files

Merge pull request #24 from doriath/admin

Fabrication + admin role
  • Loading branch information...
2 parents 1c95926 + d97b1e7 commit 7df9bfab805c5c3855e381fc520a92e6083ab05e @maciejk maciejk committed Aug 29, 2011
View
2 webfront/Gemfile
@@ -40,6 +40,8 @@ group :development, :test do
gem 'pickle'
gem 'rspec'
gem 'rspec-rails'
+ gem 'fabrication'
+ gem 'faker'
end
# group :development, :test do
# gem 'webrat'
View
5 webfront/Gemfile.lock
@@ -70,6 +70,9 @@ GEM
erubis (2.6.6)
abstract (>= 1.0.0)
eventmachine (0.12.10)
+ fabrication (1.1.0)
+ faker (0.9.5)
+ i18n (~> 0.4)
ffi (1.0.9)
gherkin (2.4.11)
json (>= 1.4.6)
@@ -157,6 +160,8 @@ DEPENDENCIES
cucumber-rails
database_cleaner
devise
+ fabrication
+ faker
haml
mongoid
pickle
View
10 webfront/Rakefile
@@ -4,4 +4,14 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
+module ::Favung
+ class Application
+ include Rake::DSL
+ end
+end
+
+module ::RakeFileUtils
+ extend Rake::FileUtilsExt
+end
+
Favung::Application.load_tasks
View
10 webfront/app/controllers/admin/application_controller.rb
@@ -0,0 +1,10 @@
+class Admin::ApplicationController < ApplicationController
+ before_filter :is_admin?
+
+ private
+ def is_admin?
+ unless current_user && current_user.admin?
+ raise "Access Denied"
+ end
+ end
+end
View
2 webfront/app/controllers/admin/tasks_controller.rb
@@ -1,4 +1,4 @@
-class Admin::TasksController < ApplicationController
+class Admin::TasksController < Admin::ApplicationController
def index
@tasks = Task.all
end
View
9 webfront/app/models/user.rb
@@ -5,4 +5,13 @@ class User
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
+ attr_accessible :email, :password, :password_confirmation
+
+ ROLES = %w[user admin]
+
+ field :role, default: 'user'
+
+ def admin?
+ role == "admin"
+ end
end
View
4 webfront/features/step_definitions/tasks_management_steps.rb
@@ -1,5 +1,5 @@
When /^I fill in the task form$/ do
- @task = Task.new(name: 'Task name', description: 'Task descrption')
+ @task = Fabricate.build(:task)
fill_in 'task_name', with: @task.name
fill_in 'task_description', with: @task.description
@@ -13,7 +13,7 @@
end
Given /^one task exists$/ do
- @task = Task.create!(name: 'Task name', description: 'Task description')
+ @task = Fabricate(:task)
end
When /^I modify the task description$/ do
View
9 webfront/features/step_definitions/user_registration_steps.rb
@@ -15,7 +15,7 @@
end
Given /^I am registered user$/ do
- @user = User.create!(email: 'foo@example.org', password: 'secret')
+ @user = Fabricate(:user)
end
When /^I follow the sign in page$/ do
@@ -45,3 +45,10 @@
page.should have_content('Sign in')
page.should have_content('Sign up')
end
+
+Given /^I am signed in as admin$/ do
+ @user = Fabricate(:user, role: 'admin')
+ And 'I am on the home page'
+ And 'I follow the sign in page'
+ And 'I fill in sign in form'
+end
View
9 webfront/features/tasks_management.feature
@@ -3,18 +3,21 @@ Feature: Task management
I want to manage tasks / problem set
Scenario: Create new task
- Given I am on the new admin task page
+ Given I am signed in as admin
+ And I am on the new admin task page
When I fill in the task form
Then the task should be created
Scenario: Edit task
- Given one task exists
+ Given I am signed in as admin
+ And one task exists
And I am on the edit page of that task
When I modify the task description
Then the task should have new description
Scenario: Delete task
- Given one task exists
+ Given I am signed in as admin
+ And one task exists
And I am on the admin tasks page
When I delete the task
Then there should be no tasks
View
29 webfront/lib/tasks/admin.rake
@@ -0,0 +1,29 @@
+def ask message
+ print message
+ STDIN.gets.chomp
+end
+
+def ask_password message
+ print message
+ system "stty -echo"
+ result = $stdin.gets.chomp
+ system "stty echo"
+ puts
+
+ return result
+end
+
+namespace :admin do
+ desc "Create admin user"
+ task :create => :environment do
+ puts "Creating admin user..."
+ email = ask('Email: ')
+ password = ask_password('Password: ')
+ password_confirmation = ask_password('Confirm password: ')
+
+ user = User.new(email: email, password: password, password_confirmation: password_confirmation)
+ user.role = "admin"
+ user.save!
+ puts "User created successfuly"
+ end
+end
View
4 webfront/spec/fabricators/task_fabricator.rb
@@ -0,0 +1,4 @@
+Fabricator(:task) do
+ name { Faker::Name.name }
+ description { Faker::Lorem.paragraph }
+end
View
4 webfront/spec/fabricators/user_fabricator.rb
@@ -0,0 +1,4 @@
+Fabricator(:user) do
+ email { Faker::Internet.email }
+ password { 'secret' }
+end

0 comments on commit 7df9bfa

Please sign in to comment.
Something went wrong with that request. Please try again.