No commit activity in last 3 years
No release in over 3 years
This gem adds support to the ActiveRecord (3.x and 4.x) for composite types.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 0.5.2
~> 2.0.1
~> 10.3
~> 3.12
~> 2.1

Runtime

>= 0.17.0
 Project Readme

<img src=“https://codeclimate.com/github/PuzzleFlow/activerecord-postgres-composite-types/badges/gpa.svg” /> <img src=“https://travis-ci.org/PuzzleFlow/activerecord-postgres-composite-types.svg?branch=master” alt=“Build Status” /> <img src=“http://inch-ci.org/github/PuzzleFlow/activerecord-postgres-composite-types.png?branch=master” alt=“Inline docs” /> <img src=“https://badge.fury.io/rb/activerecord-postgres-composite-types.svg” alt=“Gem Version” />

ActiveRecord PostgreSQL Composite Types¶ ↑

This gem adds support to the ActiveRecord (3.x and 4.x) for composite types.

One of PostgreSQL interesting feature is composite types — it basically allows to group related columns into a single type declaration like this:

create type complex as (
  number real,
  title text
);

Form this moment a type ‘complex’ is a regular PostgreSQL type and can be used in functions, column definitions, other types definition, etc. ActiveRecord, especially from version 4.0, has extended set of supported types like arrays, hstore, json, range, … But there is no support for user defined composite types.

The Goal¶ ↑

I would like to be able to access data from table like this:

create_table :foos, :id => false do |t|
  t.column :comp, :complex, default: "(0,\"\")"
end

in simple and natural way using ActiveRecord model:

class Foo < ActiveRecord::Base
end

foo = Foo.create!(:comp => {:number => 1.2, :title => "Cool!"})

or

foo = Foo.create!(:comp => [1.2, "Cool!"])

and then

foo.comp.number # => 1.2
foo.comp.title # => "Cool!"

To achieve this goal I only have to define composite type class in a following way:

require 'postgres_composite_type'
class ComplexType < PostgresCompositeType
  register_type :complex
end

This class must be loaded before any ActiveRecord model is instantializated, but NOT before ActiveRecord is loaded. Load your classes with ActiveRecord hook:

ActiveSupport.on_load :active_record do
  require 'complex_type'
end

You can of course put this code in your railtie initializer:

initialize :complexy_type do
  ActiveSupport.on_load :active_record do
    require 'complex_type'
  end
end

That’s all folks.

Contributing to ActiveRecord PostgreSQL Composite Types¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2014 PuzzleFlow. See LICENSE for further details.