Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
reorganized railties, added coder default, added coder test
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel committed Feb 4, 2012
1 parent 5b59939 commit 3e6df6b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 66 deletions.
54 changes: 2 additions & 52 deletions lib/activerecord-postgres-hstore.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,4 @@
require 'rails'
require 'rails/generators'
require 'rails/generators/migration'
require 'pg'

# = Hstore Railtie
#
# Creates a new railtie for 2 reasons:
#
# * Initialize ActiveRecord properly
# * Add hstore:setup generator
class Hstore < Rails::Railtie

initializer 'activerecord-postgres-hstore' do
ActiveSupport.on_load :active_record do
require "activerecord-postgres-hstore/activerecord"
end
end

# Creates the hstore:setup generator. This generator creates a migration that
# adds hstore support for your database. If fact, it's just the sql from the
# contrib inside a migration. But it' s handy, isn't it?
#
# To use your generator, simply run it in your project:
#
# rails g hstore:setup
class Setup < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
pgversion = ActiveRecord::Base.connection.send(:postgresql_version)
if pgversion < 90100
migration_template 'setup_hstore.rb', 'db/migrate/setup_hstore.rb'
else
migration_template 'setup_hstore91.rb', 'db/migrate/setup_hstore.rb'
end
end

end
end
require "activerecord-postgres-hstore/railties"
require "activerecord-postgres-hstore/string"
require "activerecord-postgres-hstore/hash"
require "activerecord-postgres-hstore/coder"
14 changes: 0 additions & 14 deletions lib/activerecord-postgres-hstore/activerecord.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
module ActiveRecord
module Coders
class Hstore
def self.load(hstore)
hstore.nil? ? nil : hstore.from_hstore
end

def self.dump(obj)
obj.nil? ? nil : obj.to_hstore
end
end
end
end

# Extends AR to add Hstore functionality.
module ActiveRecord

Expand Down
26 changes: 26 additions & 0 deletions lib/activerecord-postgres-hstore/coder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module ActiveRecord
module Coders
class Hstore
def self.load(hstore)
new.load(hstore)
end

def self.dump(hstore)
new.dump(hstore)
end

def initialize(default=nil)
@default=default
end

def dump(obj)
obj.nil? ? (@default.nil? ? nil : @default.to_hstore) : obj.to_hstore
end

def load(hstore)
hstore.nil? ? nil : hstore.from_hstore
end
end
end
end

53 changes: 53 additions & 0 deletions lib/activerecord-postgres-hstore/railties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'rails'
require 'rails/generators'
require 'rails/generators/migration'
require 'pg'

# = Hstore Railtie
#
# Creates a new railtie for 2 reasons:
#
# * Initialize ActiveRecord properly
# * Add hstore:setup generator
class Hstore < Rails::Railtie

initializer 'activerecord-postgres-hstore' do
ActiveSupport.on_load :active_record do
require "activerecord-postgres-hstore/activerecord"
end
end

# Creates the hstore:setup generator. This generator creates a migration that
# adds hstore support for your database. If fact, it's just the sql from the
# contrib inside a migration. But it' s handy, isn't it?
#
# To use your generator, simply run it in your project:
#
# rails g hstore:setup
class Setup < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
pgversion = ActiveRecord::Base.connection.send(:postgresql_version)
if pgversion < 90100
migration_template 'setup_hstore.rb', 'db/migrate/setup_hstore.rb'
else
migration_template 'setup_hstore91.rb', 'db/migrate/setup_hstore.rb'
end
end

end
end

23 changes: 23 additions & 0 deletions spec/activerecord-coders-hstore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "ActiverecordCodersHstore" do
it 'should load nil' do
ActiveRecord::Coders::Hstore.load(nil).should be_nil
end

it 'should load an hstore' do
ActiveRecord::Coders::Hstore.load("a=>a").should == { 'a' => 'a' }
end

it 'should dump an hstore' do
ActiveRecord::Coders::Hstore.dump({'a'=>'a'}).should == {'a'=>'a'}.to_hstore
end

it 'should dump nil' do
ActiveRecord::Coders::Hstore.dump(nil).should be_nil
end

it 'should dump the default given nil' do
ActiveRecord::Coders::Hstore.new({'a'=>'a'}).dump(nil).should == {'a'=>'a'}.to_hstore
end
end

0 comments on commit 3e6df6b

Please sign in to comment.