Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Accessing a session globally
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Phrogz  
View profile  
 More options Apr 27 2010, 10:46 pm
From: Phrogz <phr...@mac.com>
Date: Tue, 27 Apr 2010 19:46:08 -0700 (PDT)
Local: Tues, Apr 27 2010 10:46 pm
Subject: Accessing a session globally
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.