Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Dugan committed Apr 5, 2010
1 parent 597fd38 commit 1684a0a
Show file tree
Hide file tree
Showing 15 changed files with 599 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
*.gem
26 changes: 26 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2010 Coroutine LLC

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

--------------------------------------------------------------------

acts_as_current was inspired by sentient_user.

Copyright (c) 2009 bokmann, also released under the MIT license.
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= acts_as_auditable
===================

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2010 {Coroutine LLC}[http://coroutine], released under the MIT license
46 changes: 46 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'jeweler'


desc 'Default: run tests.'
task :default => [:test]


desc 'Test the plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end


desc 'Generate documentation for the plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'acts_as_current'
rdoc.options << '--line-numbers --inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end


begin
Jeweler::Tasks.new do |gemspec|
gemspec.authors = ["Coroutine", "John Dugan"]
gemspec.description = "This acts_as extension provides a class method that allows models, controllers, and helpers access to the current instance of the model (however the request defines current)."
gemspec.email = "gem@coroutine.com"
gemspec.homepage = "http://github.com/coroutine/acts_as_current"
gemspec.name = "acts_as_current"
gemspec.summary = "Gem version of acts_as_current Rails plugin."

gemspec.add_dependency("activerecord")
gemspec.add_dependency("activesupport")
gemspec.files.include("lib/**/*.rb")
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

53 changes: 53 additions & 0 deletions acts_as_current.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{acts_as_current}
s.version = "0.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Coroutine", "John Dugan"]
s.date = %q{2010-04-05}
s.description = %q{This acts_as extension provides a class method that allows models, controllers, and helpers access to the current instance of the model (however the request defines current).}
s.email = %q{gem@coroutine.com}
s.extra_rdoc_files = [
"README"
]
s.files = [
"VERSION",
"lib/acts_as_current.rb",
"lib/acts_as_current/base.rb",
"lib/acts_as_current/class_methods.rb",
"lib/acts_as_current/instance_methods.rb"
]
s.homepage = %q{http://github.com/coroutine/acts_as_current}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{Gem version of acts_as_current Rails plugin.}
s.test_files = [
"test/acts_as_current/base_test.rb",
"test/acts_as_current/simple_test.rb",
"test/acts_as_current/sti_test.rb",
"test/test_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
else
s.add_dependency(%q<activerecord>, [">= 0"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
else
s.add_dependency(%q<activerecord>, [">= 0"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
end

1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require File.dirname(__FILE__) + "/rails/init.rb"
12 changes: 12 additions & 0 deletions lib/acts_as_current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# external gems
require "active_record"


# acts_as_current extension
require File.dirname(__FILE__) + "/acts_as_current/base"
require File.dirname(__FILE__) + "/acts_as_current/class_methods"
require File.dirname(__FILE__) + "/acts_as_current/instance_methods"


# add extensions to active record
::ActiveRecord::Base.send(:include, Coroutine::ActsAsCurrent::Base)
59 changes: 59 additions & 0 deletions lib/acts_as_current/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module Coroutine

# This module is an acts_as extension that teaches an ActiveRecord model how to provide a reference
# to the instance owned by the current thread through a class method.
#
# The module includes class methods and instance methods that simplify the process of storing the
# current reference in a thread safe manner.
#
# Because acts_as_current relies on the Thread.current hash, it should probably be used sparingly.
#
module ActsAsCurrent

# This module provides the base functionality for the acts_as_current extension. It declares the
# class method acts_as_current and handles including all necessary sub modules when the class
# method is invoked.
#
module Base

def self.included(klass) #:nodoc:
klass.class_eval do

# This class method extends an ActiveRecord class with behavior appropriate for providing a
# current instance to the request.
#
# Including this method in a model definition adds two public class methods and two public
# instance methods to the model. See modules below for method defintions. Here's a simple
# skeleton that demonstrates the resulting interface.
#
#
# class MyClass < ActiveRecord::Base
#
# def self.current
# end
#
# def self.current=(instance)
# end
#
# def current?
# end
#
# def current!
# end
#
# end
#
def self.acts_as_current

# mixin methods
extend Coroutine::ActsAsCurrent::ClassMethods
include Coroutine::ActsAsCurrent::InstanceMethods

end

end
end

end
end
end
39 changes: 39 additions & 0 deletions lib/acts_as_current/class_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Coroutine
module ActsAsCurrent

# This module defines methods that will be mixed into the class definition when acts_as_current
# is invoked.
#
# The notation used below assumes the module will be invoked using the :extend method, ensuring
# the wrapping scope is the class object.
#
module ClassMethods

# This method returns the reference to the instance defined as current.
#
def current
Thread.current[acts_as_current_symbol]
end

# This method sets the reference to the instance defined as current.
#
def current=(instance)
unless (instance.is_a?(self) || instance.nil?)
raise(ArgumentError, "The method expected an instance of class '#{name}', but instead was given #{instance.inspect}")
end
Thread.current[acts_as_current_symbol] = instance
end


private

# This method returns the singularized, underscored symbol for the class.
#
def acts_as_current_symbol
name.underscore.to_sym
end

end

end
end
28 changes: 28 additions & 0 deletions lib/acts_as_current/instance_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Coroutine
module ActsAsCurrent

# This module defines methods that will be mixed into the instance when acts_as_current is invoked.
#
# The notation used below assumes the module will be invoked using the :include method, ensuring
# the wrapping scope is the instance object.
#
module InstanceMethods

# This method returns a boolean indicating whether or not the instance is defined
# as the current instance of the class.
#
def current?
!self.class.current.nil? && self.id == self.class.current.id
end

# This method forces the instance to become the defined current instance of the
# class.
#
def current!
self.class.current = self
end

end

end
end
1 change: 1 addition & 0 deletions rails/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "acts_as_current"
37 changes: 37 additions & 0 deletions test/acts_as_current/base_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#---------------------------------------------------------
# Requirements
#---------------------------------------------------------

# all generic stuff required by test helper
require "test/test_helper"



#---------------------------------------------------------
# Tests
#---------------------------------------------------------

class ActsAsCurrentBaseTest < ActiveSupport::TestCase

#---------------------------------------------
# setup and teardown delegations
#---------------------------------------------

def setup
setup_db
end
def teardown
teardown_db
end



#---------------------------------------------
# test composition
#---------------------------------------------

def test_composition_of_class
assert_respond_to ::ActiveRecord::Base, :acts_as_current
end

end
Loading

0 comments on commit 1684a0a

Please sign in to comment.