Skip to content

gemologist/active_record-postgre_sql-composite

Repository files navigation

<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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages