Class: Edgar::Sssn

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/edgar/sssn.rb

Overview

Edgar specific session

ActiveRecord::SessionStore::Session and Sssn are merged into Sssn because:

  • I could not find the way to build/prepare session for test;-(

  • It was verbose to Sync Sssn and Session.

See ActiveRecord::SessionStore how to do that.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) data

Lazy-unmarshal session state.



68
69
70
# File 'app/models/edgar/sssn.rb', line 68

def data
  @data ||= self.class.unmarshal(read_attribute('data')) || {}
end

Class Method Details

+ (Object) data_column_size_limit



28
29
30
# File 'app/models/edgar/sssn.rb', line 28

def data_column_size_limit
  @data_column_size_limit ||= columns_hash['data'].limit
end

+ (Object) delete_stale_sessions(dry_run = true)

delete stale sessions no longer active than SESSION_TIME_OUT minutes ago

INPUTS

dry_run

dry-run mode (default = true)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/edgar/sssn.rb', line 108

def self.delete_stale_sessions(dry_run=true)
  self.transaction do
    for s in Sssn.all(
        :conditions=>['updated_at<?', Edgar::BaseConfig::SESSION_TIME_OUT.minutes.ago.utc]) do
      begin
        session_id = s.session_id
        s.destroy
        logger.info("deleting session(#{session_id})")
      rescue ActiveRecord::RecordNotFound => ex
        logger.warn("session not found(#{session_id})")
      end
    end
    raise ActiveRecord::Rollback if dry_run
  end
end

+ (Object) find_by_session_id(session_id)

Hook to set up sessid compatibility.



33
34
35
36
# File 'app/models/edgar/sssn.rb', line 33

def find_by_session_id(session_id)
  setup_sessid_compatibility!
  find_by_session_id(session_id)
end

+ (Object) marshal(data)



38
39
40
# File 'app/models/edgar/sssn.rb', line 38

def marshal(data)
  Base64.encode64(Marshal.dump(data)) if data
end

+ (Object) setup_sessid_compatibility! (private)

Compatibility with tables using sessid instead of session_id.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/edgar/sssn.rb', line 49

def setup_sessid_compatibility!
  # Reset column info since it may be stale.
  reset_column_information
  if columns_hash['sessid']
    def self.find_by_session_id(*args)
      find_by_sessid(*args)
    end
  
    define_method(:session_id)  { sessid }
    define_method(:session_id=) { |session_id| self.sessid = session_id }
  else
    def self.find_by_session_id(session_id)
      find :first, :conditions => {:session_id=>session_id}
    end
  end
end

+ (Object) unmarshal(data)



42
43
44
# File 'app/models/edgar/sssn.rb', line 42

def unmarshal(data)
  Marshal.load(Base64.decode64(data)) if data
end

Instance Method Details

- (Object) before_destroy



128
129
130
131
132
133
134
135
136
# File 'app/models/edgar/sssn.rb', line 128

def before_destroy
  if Edgar::Login::ENABLE_LOGGING
    a         = ActionEntry.new
    a.user    = self.user
    a.action  = Action::Login.new(:kind=>Action::Login::Kind::LOGOUT)
    a.save!
  end
  true
end

- (Boolean) loaded?

Has the session been loaded yet?

Returns:

  • (Boolean)


75
76
77
# File 'app/models/edgar/sssn.rb', line 75

def loaded?
  !!@data
end

- (Object) marshal_data! (private)



80
81
82
83
# File 'app/models/edgar/sssn.rb', line 80

def marshal_data!
  write_attribute('data', self.class.marshal(self.data)) if loaded?
  true  # to continue saving other attributes(e.g. user_id) even not loaded
end

- (Object) name



124
125
126
# File 'app/models/edgar/sssn.rb', line 124

def name
  user.name + ' session'
end

- (Object) raise_on_session_data_overflow! (private)

Ensures that the data about to be stored in the database is not larger than the data storage column. Raises ActionController::SessionOverflowError.



88
89
90
91
92
93
94
95
96
# File 'app/models/edgar/sssn.rb', line 88

def raise_on_session_data_overflow!
  if loaded?
    limit = self.class.data_column_size_limit
    if limit and read_attribute('data').size > limit
      raise ActionController::SessionOverflowError
    end
  end
  true  # to continue saving other attributes(e.g. user_id) even not loaded
end