Nicolae Claudius
13 years ago
27 changed files with 510 additions and 34 deletions
@ -0,0 +1,12 @@
|
||||
class PermissionsController < ApplicationController |
||||
active_scaffold :permission do |conf| |
||||
conf.actions.exclude :show |
||||
conf.columns = [:domain, :user, :user_email] |
||||
conf.list.columns = [:domain, :user, :user_email] |
||||
conf.create.columns = [:domain, :user_email] |
||||
conf.update.columns = [:domain, :user_email] |
||||
conf.columns[:user_email].form_ui = :virtual |
||||
conf.columns[:user_email].description = 'user\'s email address, to share with. Ex. jhon.doe@domain.com' |
||||
end |
||||
before_filter :ensure_nested_under_domain |
||||
end |
@ -0,0 +1,30 @@
|
||||
class Permission < ActiveRecord::Base |
||||
belongs_to :domain, :inverse_of => :permissions |
||||
belongs_to :user, :inverse_of => :permissions |
||||
|
||||
validates :domain_id, :presence => true |
||||
validates :user_id, :presence => true, :uniqueness => { |
||||
:scope => :domain_id, |
||||
:message => "already is permitted" |
||||
} |
||||
validates :user, :presence => { |
||||
:if => lambda {@user_email.present?}, |
||||
:message => "with given email was not found" |
||||
} |
||||
validate do |
||||
errors[:user] = 'cannot be yourself' if user_id == domain.user_id |
||||
end |
||||
|
||||
def user_email |
||||
@user_email || user.try(:email) |
||||
end |
||||
|
||||
def user_email=(email) |
||||
@user_email = email |
||||
self.user = User.find_by_email(email) |
||||
end |
||||
|
||||
def to_label |
||||
user.try(:email) || @user_email || '-' |
||||
end |
||||
end |
@ -0,0 +1,73 @@
|
||||
# tweaks to allow Squeel Join |
||||
|
||||
module CanCan |
||||
|
||||
module ModelAdapters |
||||
class ActiveRecordAdapter < AbstractAdapter |
||||
|
||||
def tableized_conditions(conditions, model_class = @model_class) |
||||
return conditions unless conditions.kind_of? Hash |
||||
conditions.inject({}) do |result_hash, (name, value)| |
||||
reflection_name = name.kind_of?(Symbol) ? name : name._name # Squeel compatibility |
||||
if value.kind_of? Hash |
||||
reflection = model_class.reflect_on_association(reflection_name) |
||||
association_class = reflection.class_name.constantize |
||||
name = reflection.table_name.to_sym |
||||
value = tableized_conditions(value, association_class) |
||||
end |
||||
result_hash[name] = value |
||||
result_hash |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
# override to fix overwrites |
||||
# do not write existing hashes using empty hashes |
||||
def merge_joins(base, add) |
||||
add.each do |name, nested| |
||||
if base[name].is_a?(Hash) && nested.present? |
||||
merge_joins(base[name], nested) |
||||
elsif !base[name].is_a?(Hash) || nested.present? |
||||
base[name] = nested |
||||
end |
||||
end |
||||
end |
||||
|
||||
end |
||||
end |
||||
|
||||
class Rule |
||||
# allow Squeel Join |
||||
def matches_conditions_hash?(subject, conditions = @conditions) |
||||
if conditions.empty? |
||||
true |
||||
else |
||||
if model_adapter(subject).override_conditions_hash_matching? subject, conditions |
||||
model_adapter(subject).matches_conditions_hash? subject, conditions |
||||
else |
||||
conditions.all? do |name, value| |
||||
if model_adapter(subject).override_condition_matching? subject, name, value |
||||
model_adapter(subject).matches_condition? subject, name, value |
||||
else |
||||
method_name = name.kind_of?(Symbol) ? name : name._name # Squeel compatibility |
||||
attribute = subject.send(method_name) |
||||
if value.kind_of?(Hash) |
||||
if attribute.kind_of? Array |
||||
attribute.any? { |element| matches_conditions_hash? element, value } |
||||
else |
||||
!attribute.nil? && matches_conditions_hash?(attribute, value) |
||||
end |
||||
elsif value.kind_of?(Array) || value.kind_of?(Range) |
||||
value.include? attribute |
||||
else |
||||
attribute == value |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
end |
@ -0,0 +1,12 @@
|
||||
Squeel.configure do |config| |
||||
# To load hash extensions (to allow for AND (&), OR (|), and NOT (-) against |
||||
# hashes of conditions) |
||||
config.load_core_extensions :hash |
||||
|
||||
# To load symbol extensions (for a subset of the old MetaWhere functionality, |
||||
# via ARel predicate methods on Symbols: :name.matches, etc) |
||||
# config.load_core_extensions :symbol |
||||
|
||||
# To load both hash and symbol extensions |
||||
config.load_core_extensions :hash, :symbol |
||||
end |
@ -0,0 +1,12 @@
|
||||
class CreatePermissions < ActiveRecord::Migration |
||||
def change |
||||
create_table :permissions do |t| |
||||
t.references :domain # the domain to whom permission is being given |
||||
t.references :user # the user permitted to manage |
||||
|
||||
t.timestamps |
||||
end |
||||
add_index :permissions, :domain_id |
||||
add_index :permissions, :user_id |
||||
end |
||||
end |
@ -0,0 +1,157 @@
|
||||
require 'spec_helper' |
||||
|
||||
# This spec was generated by rspec-rails when you ran the scaffold generator. |
||||
# It demonstrates how one might use RSpec to specify the controller code that |
||||
# was generated by Rails when you ran the scaffold generator. |
||||
# |
||||
# It assumes that the implementation code is generated by the rails scaffold |
||||
# generator. If you are using any extension libraries to generate different |
||||
# controller code, this generated spec may or may not pass. |
||||
# |
||||
# It only uses APIs available in rails and/or rspec-rails. There are a number |
||||
# of tools you can use to make these specs even more expressive, but we're |
||||
# sticking to rails and rspec-rails APIs to keep things simple and stable. |
||||
# |
||||
# Compared to earlier versions of this generator, there is very limited use of |
||||
# stubs and message expectations in this spec. Stubs are only used when there |
||||
# is no simpler way to get a handle on the object needed for the example. |
||||
# Message expectations are only used when there is no simpler way to specify |
||||
# that an instance is receiving a specific message. |
||||
|
||||
describe PermissionsController do |
||||
|
||||
# This should return the minimal set of attributes required to create a valid |
||||
# Permission. As you add validations to Permission, be sure to |
||||
# update the return value of this method accordingly. |
||||
def valid_attributes |
||||
{} |
||||
end |
||||
|
||||
describe "GET index" do |
||||
it "assigns all permissions as @permissions" do |
||||
permission = Permission.create! valid_attributes |
||||
get :index |
||||
assigns(:permissions).should eq([permission]) |
||||
end |
||||
end |
||||
|
||||
describe "GET show" do |
||||
it "assigns the requested permission as @permission" do |
||||
permission = Permission.create! valid_attributes |
||||
get :show, :id => permission.id.to_s |
||||
assigns(:permission).should eq(permission) |
||||
end |
||||
end |
||||
|
||||
describe "GET new" do |
||||
it "assigns a new permission as @permission" do |
||||
get :new |
||||
assigns(:permission).should be_a_new(Permission) |
||||
end |
||||
end |
||||
|
||||
describe "GET edit" do |
||||
it "assigns the requested permission as @permission" do |
||||
permission = Permission.create! valid_attributes |
||||
get :edit, :id => permission.id.to_s |
||||
assigns(:permission).should eq(permission) |
||||
end |
||||
end |
||||
|
||||
describe "POST create" do |
||||
describe "with valid params" do |
||||
it "creates a new Permission" do |
||||
expect { |
||||
post :create, :permission => valid_attributes |
||||
}.to change(Permission, :count).by(1) |
||||
end |
||||
|
||||
it "assigns a newly created permission as @permission" do |
||||
post :create, :permission => valid_attributes |
||||
assigns(:permission).should be_a(Permission) |
||||
assigns(:permission).should be_persisted |
||||
end |
||||
|
||||
it "redirects to the created permission" do |
||||
post :create, :permission => valid_attributes |
||||
response.should redirect_to(Permission.last) |
||||
end |
||||
end |
||||
|
||||
describe "with invalid params" do |
||||
it "assigns a newly created but unsaved permission as @permission" do |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Permission.any_instance.stub(:save).and_return(false) |
||||
post :create, :permission => {} |
||||
assigns(:permission).should be_a_new(Permission) |
||||
end |
||||
|
||||
it "re-renders the 'new' template" do |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Permission.any_instance.stub(:save).and_return(false) |
||||
post :create, :permission => {} |
||||
response.should render_template("new") |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "PUT update" do |
||||
describe "with valid params" do |
||||
it "updates the requested permission" do |
||||
permission = Permission.create! valid_attributes |
||||
# Assuming there are no other permissions in the database, this |
||||
# specifies that the Permission created on the previous line |
||||
# receives the :update_attributes message with whatever params are |
||||
# submitted in the request. |
||||
Permission.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) |
||||
put :update, :id => permission.id, :permission => {'these' => 'params'} |
||||
end |
||||
|
||||
it "assigns the requested permission as @permission" do |
||||
permission = Permission.create! valid_attributes |
||||
put :update, :id => permission.id, :permission => valid_attributes |
||||
assigns(:permission).should eq(permission) |
||||
end |
||||
|
||||
it "redirects to the permission" do |
||||
permission = Permission.create! valid_attributes |
||||
put :update, :id => permission.id, :permission => valid_attributes |
||||
response.should redirect_to(permission) |
||||
end |
||||
end |
||||
|
||||
describe "with invalid params" do |
||||
it "assigns the permission as @permission" do |
||||
permission = Permission.create! valid_attributes |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Permission.any_instance.stub(:save).and_return(false) |
||||
put :update, :id => permission.id.to_s, :permission => {} |
||||
assigns(:permission).should eq(permission) |
||||
end |
||||
|
||||
it "re-renders the 'edit' template" do |
||||
permission = Permission.create! valid_attributes |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Permission.any_instance.stub(:save).and_return(false) |
||||
put :update, :id => permission.id.to_s, :permission => {} |
||||
response.should render_template("edit") |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "DELETE destroy" do |
||||
it "destroys the requested permission" do |
||||
permission = Permission.create! valid_attributes |
||||
expect { |
||||
delete :destroy, :id => permission.id.to_s |
||||
}.to change(Permission, :count).by(-1) |
||||
end |
||||
|
||||
it "redirects to the permissions list" do |
||||
permission = Permission.create! valid_attributes |
||||
delete :destroy, :id => permission.id.to_s |
||||
response.should redirect_to(permissions_url) |
||||
end |
||||
end |
||||
|
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
# Specs in this file have access to a helper object that includes |
||||
# the PermissionsHelper. For example: |
||||
# |
||||
# describe PermissionsHelper do |
||||
# describe "string concat" do |
||||
# it "concats two strings with spaces" do |
||||
# helper.concat_strings("this","that").should == "this that" |
||||
# end |
||||
# end |
||||
# end |
||||
describe PermissionsHelper do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
@ -0,0 +1,5 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe Permission do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
@ -0,0 +1,11 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "Permissions" do |
||||
describe "GET /permissions" do |
||||
it "works! (now write some real specs)" do |
||||
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers |
||||
get permissions_path |
||||
response.status.should be(200) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,35 @@
|
||||
require "spec_helper" |
||||
|
||||
describe PermissionsController do |
||||
describe "routing" do |
||||
|
||||
it "routes to #index" do |
||||
get("/permissions").should route_to("permissions#index") |
||||
end |
||||
|
||||
it "routes to #new" do |
||||
get("/permissions/new").should route_to("permissions#new") |
||||
end |
||||
|
||||
it "routes to #show" do |
||||
get("/permissions/1").should route_to("permissions#show", :id => "1") |
||||
end |
||||
|
||||
it "routes to #edit" do |
||||
get("/permissions/1/edit").should route_to("permissions#edit", :id => "1") |
||||
end |
||||
|
||||
it "routes to #create" do |
||||
post("/permissions").should route_to("permissions#create") |
||||
end |
||||
|
||||
it "routes to #update" do |
||||
put("/permissions/1").should route_to("permissions#update", :id => "1") |
||||
end |
||||
|
||||
it "routes to #destroy" do |
||||
delete("/permissions/1").should route_to("permissions#destroy", :id => "1") |
||||
end |
||||
|
||||
end |
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "permissions/edit.html.erb" do |
||||
before(:each) do |
||||
@permission = assign(:permission, stub_model(Permission)) |
||||
end |
||||
|
||||
it "renders the edit permission form" do |
||||
render |
||||
|
||||
# Run the generator again with the --webrat flag if you want to use webrat matchers |
||||
assert_select "form", :action => permissions_path(@permission), :method => "post" do |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,14 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "permissions/index.html.erb" do |
||||
before(:each) do |
||||
assign(:permissions, [ |
||||
stub_model(Permission), |
||||
stub_model(Permission) |
||||
]) |
||||
end |
||||
|
||||
it "renders a list of permissions" do |
||||
render |
||||
end |
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "permissions/new.html.erb" do |
||||
before(:each) do |
||||
assign(:permission, stub_model(Permission).as_new_record) |
||||
end |
||||
|
||||
it "renders new permission form" do |
||||
render |
||||
|
||||
# Run the generator again with the --webrat flag if you want to use webrat matchers |
||||
assert_select "form", :action => permissions_path, :method => "post" do |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue