My web app under development uses Sinatra and Sequel, among other
things. I use sessions to record the authenticated user who is
interacting with the application. My database models periodically
record log entries when values are changed. (Examples of this are at
the end of the message.)
My problem is that these logs need to record the user who made the
changes, but the session method is not available because I'm inside a
Sequel::Model instance, not Sinatra. See all the "active_user_id" in
the code samples below. In some cases it would be inconvenient to have
the route pass along the user id from the session (cases where I'm
currently using foo= methods to set a value). In other cases these
logs are written in a callback where I can't even pass values along if
I wanted to (see the after_create and before_save callbacks).
In Ramaze (where this application was first developed), I could use
Ramaze::Current.session inside the model to find the active session.
How can I get access to the session "globally" from outside my route
in Sinatra?
class Bug < Sequel::Model
def tag_names=( desired_tags )
# add/remove tags from a join table as necessary
BugLogEntry << {
:created_on => Time.now,
:bug_id => id,
:user_id => active_user_id,
:field_id => BugLogField::TAG_NAMES,
:new_value => tag_changes
}
end
def after_create
BUG_FIELDS.each do |field|
BugLogEntry << {
:created_on => Time.now,
:bug_id => id,
:user_id => active_user_id,
:field_id => BugLogField.const_get(field.upcase),
:new_value => self.send(field).to_s
}
end
end
def before_save
if old_bug = Bug[id]
BUG_FIELDS.each do |field|
new_value = self.send( field )
old_value = old_bug.send( field )
if new_value != old_value
BugLogEntry << {
:created_on => Time.now,
:bug_id => id,
:user_id => active_user_id,
:field_id => BugLogField.const_get(field.upcase),
:new_value => new_value,
:old_value => old_value
}
end
end
end
end
end
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sinatrarb@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.