merb_activerecord 0.9.5 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ GEM_EMAIL = "canadaduane@gmail.com"
17
17
 
18
18
  GEM_NAME = "merb_activerecord"
19
19
  PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
20
- GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.5") + PKG_BUILD
20
+ GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.6") + PKG_BUILD
21
21
 
22
22
  RELEASE_NAME = "REL #{GEM_VERSION}"
23
23
 
@@ -35,7 +35,7 @@ spec = Gem::Specification.new do |s|
35
35
  s.author = GEM_AUTHOR
36
36
  s.email = GEM_EMAIL
37
37
  s.homepage = PROJECT_URL
38
- s.add_dependency('merb-core', '>= 0.9.5')
38
+ s.add_dependency('merb-core', '>= 0.9.6')
39
39
  s.require_path = 'lib'
40
40
  s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*") end
41
41
 
@@ -45,14 +45,14 @@ end
45
45
 
46
46
  desc "Install the gem"
47
47
  task :install => [:package] do
48
- sh %{#{sudo} gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}
48
+ sh install_command(GEM_NAME, GEM_VERSION)
49
49
  end
50
50
 
51
51
  namespace :jruby do
52
52
 
53
53
  desc "Run :package and install the resulting .gem with jruby"
54
54
  task :install => :package do
55
- sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
55
+ sh jinstall_command(GEM_NAME, GEM_VERSION)
56
56
  end
57
57
 
58
58
  end
@@ -316,11 +316,6 @@ namespace :db do
316
316
  end
317
317
 
318
318
  namespace :sessions do
319
- desc "Create sessions table"
320
- task :create => :merb_start do
321
- Merb::ActiveRecordSession.create_table!
322
- end
323
-
324
319
  desc "Clear the sessions table"
325
320
  task :clear => :merb_start do
326
321
  session_table = 'session'
@@ -330,10 +325,6 @@ namespace :db do
330
325
  end
331
326
  end
332
327
 
333
- def session_table_name
334
- ActiveRecord::Base.pluralize_table_names ? :sessions : :session
335
- end
336
-
337
328
  def set_firebird_env(config)
338
329
  ENV["ISC_USER"] = config["username"].to_s if config["username"]
339
330
  ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
@@ -1,4 +1,4 @@
1
1
  Merb::Generators::SessionMigrationGenerator.template :session_migration_activerecord, :orm => :activerecord do
2
- source(File.dirname(__FILE__), 'templates/session_migration/schema/migrations/%version%_sessions.rb')
3
- destination("schema/migrations/#{version}_sessions.rb")
2
+ source(File.dirname(__FILE__), 'templates/session_migration/schema/migrations/%version%_database_sessions.rb')
3
+ destination("schema/migrations/#{version}_database_sessions.rb")
4
4
  end
@@ -3,12 +3,12 @@ class <%= class_name %> < ActiveRecord::Migration
3
3
  <% if model -%>
4
4
  create_table :<%= table_name %> do |t|
5
5
  <% attributes.each do |name, type| -%>
6
- t.<%= name %> :<%= type %>
6
+ t.<%= name %> :<%= type %>
7
7
  <% end -%>
8
-
8
+
9
9
  t.timestamps
10
10
  end
11
- <% end -%>
11
+ <% end -%>
12
12
  end
13
13
 
14
14
  def self.down
@@ -3,6 +3,7 @@ class DatabaseSessions < ActiveRecord::Migration
3
3
  create_table :sessions do |t|
4
4
  t.column :session_id, :string
5
5
  t.column :data, :text
6
+ t.column :created_at, :datetime
6
7
  end
7
8
  add_index :sessions, :session_id
8
9
  end
@@ -59,7 +59,7 @@ module Merb
59
59
  if File.exists?(config_file)
60
60
  Merb.logger.info!("Connecting to database...")
61
61
 
62
- Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }.priority = -10
62
+ Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }
63
63
 
64
64
  ::ActiveRecord::Base.verification_timeout = 14400
65
65
  ::ActiveRecord::Base.logger = Merb.logger
@@ -73,13 +73,6 @@ module Merb
73
73
  end
74
74
  end
75
75
 
76
- # Registering this ORM lets the user choose active_record as a session
77
- # in merb.yml's session_store: option.
78
- def register_session_type
79
- Merb.register_session_type("activerecord",
80
- "merb/session/active_record_session",
81
- "Using ActiveRecord database sessions")
82
- end
83
76
  end
84
77
  end
85
78
  end
@@ -1,142 +1,65 @@
1
- require "active_record"
1
+ require 'active_record'
2
+ require 'merb-core/dispatch/session'
3
+ require 'base64'
2
4
 
3
5
  module Merb
4
- module SessionMixin
5
- def setup_session
6
- before = cookies[_session_id_key]
7
- request.session, cookies[_session_id_key] = Merb::ActiveRecordSession.persist(cookies[_session_id_key])
8
- @_fingerprint = Marshal.dump(request.session.data).hash
9
- @_new_cookie = cookies[_session_id_key] != before
10
- end
11
6
 
12
- def finalize_session
13
- request.session.save if @_fingerprint != Marshal.dump(request.session.data).hash
14
- set_cookie(_session_id_key, request.session.session_id, Time.now + _session_expiry) if (@_new_cookie || request.session.needs_new_cookie)
15
- end
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
16
18
 
17
- def session_store_type
18
- "activerecord"
19
- end
20
- end # ActiveRecordMixin
21
-
22
- class ActiveRecordSession < ::ActiveRecord::Base
23
- set_table_name 'sessions'
24
- # Customizable data column name. Defaults to 'data'.
25
- cattr_accessor :data_column_name
26
- self.data_column_name = 'data'
27
- before_save :marshal_data!
28
- before_save :raise_on_session_data_overflow!
29
- attr_accessor :needs_new_cookie
30
-
19
+ serialize :data
20
+
31
21
  class << self
32
- # Generates a new session ID and creates a row for the new session in the database.
33
- def generate
34
- create(:session_id => Merb::SessionMixin::rand_uuid, :data => {})
35
- end
36
-
37
- # Gets the existing session based on the <tt>session_id</tt> available in cookies.
38
- # If none is found, generates a new session.
39
- def persist(session_id)
40
- if session_id
41
- session = find_by_session_id(session_id)
42
- end
43
- unless session
44
- session = generate
22
+
23
+ # ==== Parameters
24
+ # session_id<String>:: ID of the session to retrieve.
25
+ #
26
+ # ==== Returns
27
+ # ContainerSession:: The session corresponding to the ID.
28
+ def retrieve_session(session_id)
29
+ if item = find_by_session_id(session_id)
30
+ item.data
45
31
  end
46
- [session, session.session_id]
47
- end
48
-
49
- # Don't try to reload ARStore::Session in dev mode.
50
- def reloadable?
51
- false
52
- end
53
-
54
- def data_column_size_limit
55
- @data_column_size_limit ||= columns_hash[@@data_column_name].limit
56
32
  end
57
33
 
58
- def marshal(data) Base64.encode64(Marshal.dump(data)) if data end
59
- def unmarshal(data) Marshal.load(Base64.decode64(data)) if data end
60
-
61
- def create_table!
62
- connection.execute <<-end_sql
63
- CREATE TABLE #{table_name} (
64
- id INTEGER PRIMARY KEY,
65
- #{connection.quote_column_name('session_id')} TEXT UNIQUE,
66
- #{connection.quote_column_name(@@data_column_name)} TEXT(255)
67
- )
68
- end_sql
34
+ # ==== Parameters
35
+ # session_id<String>:: ID of the session to set.
36
+ # data<ContainerSession>:: The session to set.
37
+ def store_session(session_id, data)
38
+ if item = find_by_session_id(session_id)
39
+ item.update_attributes!(:data => data)
40
+ else
41
+ create(:session_id => session_id, :data => data)
42
+ end
69
43
  end
70
44
 
71
- def drop_table!
72
- connection.execute "DROP TABLE #{table_name}"
45
+ # ==== Parameters
46
+ # session_id<String>:: ID of the session to delete.
47
+ def delete_session(session_id)
48
+ delete_all(["#{connection.quote_column_name('session_id')} IN (?)", session_id])
73
49
  end
74
- end
75
-
76
- # Regenerate the Session ID
77
- def regenerate
78
- update_attributes(:session_id => Merb::SessionMixin::rand_uuid)
79
- self.needs_new_cookie = true
80
- end
81
-
82
- # Recreates the cookie with the default expiration time
83
- # Useful during log in for pushing back the expiration date
84
- def refresh_expiration
85
- self.needs_new_cookie = true
86
- end
87
-
88
- # Lazy-delete of session data
89
- def delete(key = nil)
90
- key ? self.data.delete(key) : self.data.clear
91
- end
92
-
93
- def [](key)
94
- data[key]
95
- end
96
-
97
- def empty?
98
- data.empty?
99
- end
100
-
101
- def each(&b)
102
- data.each(&b)
50
+
103
51
  end
104
52
 
105
- def each_with_index(&b)
106
- data.each_with_index(&b)
107
- end
53
+ end
108
54
 
109
- def []=(key, val)
110
- data[key] = val
111
- end
55
+ class ActiveRecordSession < SessionStoreContainer
112
56
 
113
- # Lazy-unmarshal session state.
114
- def data
115
- @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
116
- end
117
-
118
- # Has the session been loaded yet?
119
- def loaded?
120
- !! @data
121
- end
122
-
123
- private
124
- attr_writer :data
125
-
126
- def marshal_data!
127
- return false if !loaded?
128
- write_attribute(@@data_column_name, self.class.marshal(self.data))
129
- end
130
-
131
- # Ensures that the data about to be stored in the database is not
132
- # larger than the data storage column. Raises
133
- # ActionController::SessionOverflowError.
134
- def raise_on_session_data_overflow!
135
- return false if !loaded?
136
- limit = self.class.data_column_size_limit
137
- if loaded? and limit and read_attribute(@@data_column_name).size > limit
138
- raise MerbController::SessionOverflowError
139
- end
140
- end
141
- end # ActiveRecordSessionMixin
142
- end # Merb
57
+ # The session store type
58
+ self.session_store_type = :activerecord
59
+
60
+ # The store object is the model class itself
61
+ self.store = ActiveRecordSessionStore
62
+
63
+ end
64
+
65
+ end
@@ -1,5 +1,7 @@
1
1
  if defined?(Merb::Plugins)
2
+
2
3
  dependency "activerecord"
4
+
3
5
  require File.join(File.dirname(__FILE__) / "merb" / "orms" / "active_record" / "connection")
4
6
  Merb::Plugins.add_rakefiles(File.join(File.dirname(__FILE__) / "active_record" / "merbtasks"))
5
7
 
@@ -9,7 +11,10 @@ if defined?(Merb::Plugins)
9
11
 
10
12
  def self.run
11
13
  Merb::Orms::ActiveRecord.connect
12
- Merb::Orms::ActiveRecord.register_session_type
14
+ if Merb::Config.session_stores.include?(:activerecord)
15
+ Merb.logger.debug "Using ActiveRecord sessions"
16
+ require File.join(File.dirname(__FILE__) / "merb" / "session" / "active_record_session")
17
+ end
13
18
  end
14
19
 
15
20
  end
@@ -0,0 +1,57 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'merb-core'
3
+ require 'merb-core/test'
4
+ require 'merb-core/test/helpers'
5
+
6
+ Merb::BootLoader.before_app_loads do
7
+ require "merb/session/active_record_session"
8
+ end
9
+
10
+ Merb.start_environment( :environment => 'test', :adapter => 'runner',
11
+ :session_store => 'activerecord')
12
+
13
+ Spec::Runner.configure do |config|
14
+ config.include Merb::Test::RequestHelper
15
+ end
16
+
17
+ require 'merb_activerecord'
18
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
19
+ :dbfile => ":memory:")
20
+
21
+ ActiveRecord::Schema.define do
22
+ create_table :sessions do |t|
23
+ t.column :session_id, :string
24
+ t.column :data, :text
25
+ t.column :created_at, :datetime
26
+ end
27
+ end
28
+
29
+ # Load up the shared specs from merb-core
30
+ if (gem_spec = Gem.source_index.search('merb-core').last) &&
31
+ gem_spec.files.include?('spec/public/session/controllers/sessions.rb')
32
+ require gem_spec.full_gem_path / 'spec/public/session/controllers/sessions.rb'
33
+ require gem_spec.full_gem_path / 'spec/public/session/session_spec.rb'
34
+ end
35
+
36
+ describe Merb::ActiveRecordSession do
37
+
38
+ before do
39
+ @session_class = Merb::ActiveRecordSession
40
+ @session = @session_class.generate
41
+ end
42
+
43
+ it_should_behave_like "All session-store backends"
44
+
45
+ it "should have a session_store_type class attribute" do
46
+ @session.class.session_store_type.should == :activerecord
47
+ end
48
+
49
+ end
50
+
51
+ describe Merb::ActiveRecordSession, "mixed into Merb::Controller" do
52
+
53
+ before(:all) { @session_class = Merb::ActiveRecordSession }
54
+
55
+ it_should_behave_like "All session-stores mixed into Merb::Controller"
56
+
57
+ end
@@ -10,14 +10,12 @@ describe Merb::Orms::ActiveRecord::Connect do
10
10
  end
11
11
  end
12
12
 
13
-
14
-
15
13
  describe "Merb ActiveRecord extension" do
16
14
  before :all do
17
15
  @wd = Dir.pwd
18
16
  Merb.stub!(:dir_for).with(:config).and_return(@wd)
19
- @config_file_path = @wd / "database.yml"
20
- @sample_file_path = @wd / "database.yml.sample"
17
+ @config_file_path = @wd / "config" / "database.yml"
18
+ @sample_file_path = @wd / "config" / "database.yml.sample"
21
19
 
22
20
  @sample_source = Merb::Orms::ActiveRecord.sample_source
23
21
  @config_sample = Erubis.load_yaml_file(@sample_source)
@@ -52,11 +50,6 @@ describe "Merb ActiveRecord extension" do
52
50
  @config_sample[:development][:encoding].should == "utf8"
53
51
  end
54
52
 
55
- it "stores configurations from config file" do
56
- Erubis.should_receive(:load_yaml_file).with(@config_file_path).and_return(@config_sample)
57
- Merb::Orms::ActiveRecord.configurations[:development][:database].should == "sample_development"
58
- end
59
-
60
53
  it "provides Rack with a way to start a transcantion" do
61
54
  Merb::Orms::ActiveRecord.should respond_to(:open_sandbox!)
62
55
  end
data/specs/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  $TESTING = true
2
2
  $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
3
  require 'merb-core'
4
- require 'merb_activerecord'
5
- require 'merb/test/model_helper/active_record'
4
+ require 'merb_activerecord'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb_activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duane Johnson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-21 00:00:00 +03:00
12
+ date: 2008-09-08 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.5
23
+ version: 0.9.6
24
24
  version:
25
25
  description: Merb plugin that provides ActiveRecord support for Merb
26
26
  email: canadaduane@gmail.com
@@ -66,7 +66,7 @@ files:
66
66
  - lib/generators/templates/session_migration
67
67
  - lib/generators/templates/session_migration/schema
68
68
  - lib/generators/templates/session_migration/schema/migrations
69
- - lib/generators/templates/session_migration/schema/migrations/%version%_sessions.rb
69
+ - lib/generators/templates/session_migration/schema/migrations/%version%_database_sessions.rb
70
70
  - lib/merb
71
71
  - lib/merb/orms
72
72
  - lib/merb/orms/active_record
@@ -75,6 +75,7 @@ files:
75
75
  - lib/merb/session
76
76
  - lib/merb/session/active_record_session.rb
77
77
  - lib/merb_activerecord.rb
78
+ - specs/merb_active_record_session_spec.rb
78
79
  - specs/merb_active_record_spec.rb
79
80
  - specs/spec_helper.rb
80
81
  has_rdoc: true