This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganized railties, added coder default, added coder test
- Loading branch information
Joel
committed
Feb 4, 2012
1 parent
5b59939
commit 3e6df6b
Showing
5 changed files
with
104 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |