Module: DataMapper::Actionstamps
- Defined in:
- lib/dm-actionstamps/version.rb,
lib/dm-actionstamps/actionstamps.rb
Defined Under Namespace
Modules: ClassMethods, ReceiverMethods
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#actionstamps(*args) ⇒ Object
The Receiver Model receives the actionstamps and defines the.
-
#provides_actionstamps ⇒ Object
Provider method, used to setup the
current_user
information.
Instance Method Details
#actionstamps(*args) ⇒ Object
The Receiver Model receives the actionstamps and defines the
Examples
class Bill
include DataMapper::Resource
property :id, Serial
property :amount, Integer
...<snip>
actionstamps :by, Client
end
=> creates the :created_by and :updated_by fields.
actionstamps :by_id, Author
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dm-actionstamps/actionstamps.rb', line 69 def actionstamps(*args) # set default args if none passed in args = [:by, ::User ] if args.empty? # if invalid args, just bail out if ( args[0].is_a?(Hash) || nil ) || ( args[1].is_a?(Hash) || args[1].is_a?( Symbol) || args[1].is_a?(String) || nil ) raise ArgumentError, "Invalid arguments passed: syntax: actionstamps :by, ModelName" end configs = { :suffix => args[0], :model => args[1] } # do we have the fields :created_? / :updated_? declared, if not we declare them if properties.any? { |p| p.name.to_s =~ /^(created|updated)_#{Regexp.escape(configs[:suffix].to_s)}$/ } # if they are declared then we use them raise ArgumentError, "Full functionality NOT implemented yet. Please DO NOT declare your created_#{configs[:suffix]} / updated_#{configs[:suffix]} properties, they will be automatically declared by this plugin." else property "created_#{configs[:suffix]}".to_sym, Integer#, :default => 1 property "updated_#{configs[:suffix]}".to_sym, Integer#, :default => 1 end @actionstamps_receiver_properties = ["created_#{configs[:suffix]}".to_sym, "updated_#{configs[:suffix]}".to_sym ] @actionstamps_model_class = configs[:model].to_s.capitalize extend DataMapper::Actionstamps::ClassMethods include DataMapper::Actionstamps::ReceiverMethods class_eval(<<-END, __FILE__, __LINE__) def set_actionstamps self.created_#{configs[:suffix].to_s} = #{configs[:model]}.current_#{configs[:model].to_s.downcase}.id if #{configs[:model]}.current_#{configs[:model].to_s.downcase} && self.new? && self.created_#{configs[:suffix].to_s}.nil? self.updated_#{configs[:suffix].to_s} = #{configs[:model]}.current_#{configs[:model].to_s.downcase}.id if #{configs[:model]}.current_#{configs[:model].to_s.downcase} end END end |
#provides_actionstamps ⇒ Object
Provider method, used to setup the current_user
information.
Examples
class User
<snip...>
provides_actionstamps
end
=> creates the :current_user & :current_user= methods
class Client
<snip...>
provides_actionstamps
end
=> creates the :current_client & :current_client= methods
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dm-actionstamps/actionstamps.rb', line 32 def provides_actionstamps @actionstamps_class = self extend DataMapper::Actionstamps::ClassMethods class_eval(<<-END, __FILE__, __LINE__) def self.current_#{name.downcase}=(user) Thread.current["#{name.downcase}_#{self.object_id}_actionstamp"] = user end def self.current_#{name.downcase} Thread.current["#{name.downcase}_#{self.object_id}_actionstamp"] end END end |