Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. require 'active_record'
  2. require 'merb-core/dispatch/session'
  3. require 'base64'
  4.  
  5. module Merb
  6.  
  7. # Sessions stored in ActiveRecord model.
  8. #
  9. # To use ActiveRecord based sessions add the following to config/init.rb:
  10. #
  11. # Merb::Config[:session_store] = 'activerecord'
  12.  
  13. class ActiveRecordSessionStore < ::ActiveRecord::Base
  14.  
  15. table_name = (Merb::Plugins.config[:merb_active_record][:session_table_name] || "sessions")
  16.  
  17. set_table_name table_name
  18.  
  19. # Customizable data column name. Defaults to 'data'.
  20. cattr_accessor :data_column_name
  21. self.data_column_name = 'data'
  22.  
  23. before_save :marshal_data!
  24. before_save :raise_on_session_data_overflow!
  25.  
  26. class << self
  27.  
  28. # ==== Parameters
  29. # session_id<String>:: ID of the session to retrieve.
  30. #
  31. # ==== Returns
  32. # ContainerSession:: The session corresponding to the ID.
  33. def retrieve_session(session_id)
  34. if item = find_by_session_id(session_id)
  35. item.data
  36. end
  37. end
  38.  
  39. # ==== Parameters
  40. # session_id<String>:: ID of the session to set.
  41. # data<ContainerSession>:: The session to set.
  42. def store_session(session_id, data)
  43. if item = find_by_session_id(session_id)
  44. item.update_attributes!(:data => data)
  45. else
  46. create(:session_id => session_id, :data => data)
  47. end
  48. end
  49.  
  50. # ==== Parameters
  51. # session_id<String>:: ID of the session to delete.
  52. def delete_session(session_id)
  53. delete_all(["#{connection.quote_column_name('session_id')} IN (?)", session_id])
  54. end
  55.  
  56. # ==== Returns
  57. # Integer:: The maximum length of the 'data' column.
  58. def data_column_size_limit
  59. @data_column_size_limit ||= columns_hash[@@data_column_name].limit
  60. end
  61.  
  62. def marshal(data)
  63. Base64.encode64(Marshal.dump(data)) if data
  64. end
  65.  
  66. def unmarshal(data)
  67. Marshal.load(Base64.decode64(data)) if data
  68. end
  69.  
  70. end
  71.  
  72. # Lazy-unmarshal session state.
  73. def data
  74. @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
  75. end
  76.  
  77. # Virtual attribute writer - override.
  78. def data=(hsh)
  79. @data = hsh if hsh.is_a?(Hash)
  80. end
  81.  
  82. # Has the session been loaded yet?
  83. def loaded?
  84. !!@data
  85. end
  86.  
  87. private
  88.  
  89. # Marshals data before saving.
  90. def marshal_data!
  91. return false if !loaded?
  92. write_attribute(@@data_column_name, self.class.marshal(self.data))
  93. end
  94.  
  95. # Ensures that the data about to be stored in the database is not
  96. # larger than the data storage column.
  97. #
  98. # ==== Raises
  99. # Merb::SessionMixin::SessionOverflow:: raised if there's too much data.
  100. def raise_on_session_data_overflow!
  101. return false if !loaded?
  102. limit = self.class.data_column_size_limit
  103. if loaded? && limit && read_attribute(@@data_column_name).size > limit
  104. raise Merb::SessionMixin::SessionOverflow
  105. end
  106. end
  107.  
  108. end
  109.  
  110. class ActiveRecordSession < SessionStoreContainer
  111.  
  112. # The session store type
  113. self.session_store_type = :activerecord
  114.  
  115. # The store object is the model class itself
  116. self.store = ActiveRecordSessionStore
  117.  
  118. end
  119.  
  120. end
Add Comment
Please, Sign In to add comment