Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proxy class methods caching. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions lib/attr_ability/model/instance_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
module AttrAbility
module Model
class InstanceProxy
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
module InstanceProxy
@@initialization_mutex = Mutex.new

def initialize(model, sanitizer)
@model = model
@sanitizer = sanitizer
def self.[](klass)
@@initialization_mutex.synchronize do
@_proxy_classes ||= {}
@_proxy_classes[klass] ||= Class.new do
include ProxyMethods
end
end
end

protected
module ProxyMethods
extend ActiveSupport::Concern

def method_missing(name, *args, &block)
@model.mass_assignment_sanitizer = @sanitizer
@model.send(name, *args, &block)
ensure
@model.mass_assignment_sanitizer = nil
included do
alias __proxy_class__ class
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$|^initialize$|^method_missing$)/ }
end

def initialize(model, sanitizer)
@model = model
@sanitizer = sanitizer
end

protected

def method_missing(name, *args, &block)
delegate_method(name)
self.send(name, *args, &block)
end

private

def delegate_method(name)
__proxy_class__.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{name}(*args, &block)
@model.mass_assignment_sanitizer = @sanitizer
@model.#{name}(*args, &block)
ensure
@model.mass_assignment_sanitizer = nil
end
METHOD
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/attr_ability/model_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def ability(action, attributes)
end

def as(ability)
AttrAbility::Model::InstanceProxy.new(self, self.class.build_sanitizer(ability))
AttrAbility::Model::InstanceProxy[self.class].new(self, self.class.build_sanitizer(ability))
end

def as_system
Expand Down
38 changes: 38 additions & 0 deletions spec/attr_ability/model/instance_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe AttrAbility::Model::InstanceProxy do
with_model :ProxyPost do
table do |t|
t.string :title
t.integer :author_id
t.string :status
end
end

with_model :ProxyAuthor do
table do |t|
t.string :name
end
end

subject { AttrAbility::Model::InstanceProxy }
let(:sanitizer) { AttrAbility::Model::Sanitizer.new(SanitizerTestAbility.new(:system)) }
let(:post) { ProxyPost.new(title: "some") }
let(:author) { ProxyAuthor.new(name: "some") }
let(:proxied_post) { subject[ProxyPost].new(post, sanitizer) }
let(:proxied_author) { subject[ProxyAuthor].new(author, sanitizer) }

it "caches class" do
subject[ProxyPost] == subject[ProxyPost]
end

it "proxies methods" do
proxied_post.title.should == post.title
end

it "does not leak methods to another model" do
# ensure post cached
proxied_post.title
proxied_author.methods.should_not include(:title)
end
end