Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also enables cancan and improves testing.
- Loading branch information
Showing
18 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CharactersController < ApplicationController | ||
expose :character, attributes: :character_params | ||
expose(:characters){ current_user.characters } | ||
load_and_authorize_resource | ||
|
||
private | ||
|
||
def character_params | ||
params.require(:character).permit(:name) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Ability | ||
include CanCan::Ability | ||
|
||
def initialize(user) | ||
user ||= User.new | ||
|
||
can :access, :sessions | ||
can :read, :application | ||
can :read, :characters | ||
|
||
# Registered users | ||
if user.persisted? | ||
can :update, :users, id: user.id | ||
|
||
if user.valid? | ||
can :update, :characters, user_id: user.id | ||
can :access, :users, id: user.id | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Character | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
field :name, type: String | ||
|
||
belongs_to :user | ||
|
||
validates :name, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.container | ||
.row | ||
.span12 | ||
%h1= t(".title") | ||
%p.lead= t(".lead") | ||
|
||
.row | ||
.span12 | ||
.media | ||
- characters.each do |character| | ||
= link_to [:edit, character], class: "pull-left" do | ||
%img(data-source="http://placehold.it/64x64" class="media-object") | ||
.media-body | ||
= link_to [:edit, character] do | ||
%h2= character.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module CanCan | ||
class ControllerResource | ||
|
||
# Extends CanCan in such a way that it supports the way that decent | ||
# exposure sets collection variables. | ||
# | ||
def collection_instance | ||
collection_name = instance_name.to_s.pluralize | ||
if @controller.instance_variable_defined? "@#{collection_name}" | ||
@controller.instance_variable_get("@#{collection_name}") | ||
elsif @controller.respond_to?(collection_name, true) | ||
@controller.send(collection_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
describe CharactersController do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Read about factories at https://github.com/thoughtbot/factory_girl | ||
|
||
FactoryGirl.define do | ||
factory :character do | ||
name { Faker::Name.name } | ||
user nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "spec_helper" | ||
|
||
describe "Listing characters" do | ||
let(:user) { create :user, :with_characters, :mock_omniauth } | ||
let(:other_user) { create :user, :with_characters } | ||
|
||
before do | ||
user # execute the block to stub the user | ||
visit sign_in_path | ||
click_link "twitter_sign_in" | ||
visit characters_path | ||
end | ||
|
||
describe "page" do | ||
subject{ page } | ||
|
||
it { should have_content I18n.translate("characters.index.title") } | ||
it "should have all the user's characters" do | ||
user.characters.each do |character| | ||
should have_content character.name | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "spec_helper" | ||
require "cancan/matchers" | ||
|
||
describe Ability do | ||
subject{ Ability.new(user) } | ||
|
||
describe "as guest" do | ||
let(:user) { build :user } | ||
|
||
it { should be_able_to :access, :sessions } | ||
it { should be_able_to :read, :application } | ||
it { should_not be_able_to :access, :characters } | ||
it { should_not be_able_to :update, :user } | ||
end | ||
|
||
describe "as registered, incomplete user" do | ||
let(:user) { u = build(:user, email: nil); u.save(validate: false); u } | ||
|
||
it { should be_able_to :access, :sessions } | ||
it { should_not be_able_to :access, :characters } | ||
it { should be_able_to :update, user } | ||
end | ||
|
||
describe "as registered, complete user" do | ||
let(:user) { create :user } | ||
let(:other_user) { create :user } | ||
|
||
it { should be_able_to :access, :sessions } | ||
it { should be_able_to :update, :characters, user_id: user.id } | ||
it { should be_able_to :update, build(:character, user: user) } | ||
it { should_not be_able_to :update, build(:character, user: other_user) } | ||
it { should be_able_to :access, user } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
describe Character do | ||
subject{ build :character } | ||
|
||
# Fields | ||
it { should be_timestamped_document } | ||
it { should have_field(:name).with_default_value_of(nil) } | ||
|
||
# Validations | ||
it { should be_valid } | ||
it { should validate_presence_of(:name) } | ||
|
||
# Associations | ||
it { should belong_to :user } | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters