There will be updates to postgres_ext coming soon to make it 4.1 compatible
If it is a bug please open an issue on
Github. If you need
help using the gem please ask the question on
Stack Overflow. Be sure to tag the
question with DockYard
so we can find it.
Add this line to your application's Gemfile:
gem 'postgres_ext-serializers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install postgres_ext-serializers
Just require 'postgres_ext/serializers'
and use
ActiveModel::Serializers as you normally would!
Postgres_ext-serializers will take over anytime you try to serialize an
ActiveRecord::Relation.
If you are using methods to compute properties for your JSON responses
in your models or serializers, postgres_ext-serializers will try to
discover a SQL version of this call by looking for a class method with
the same name and the prefex __sql
. Here's an example:
class MySerializer < ActiveModel::Serializer
def full_name
"#{object.first_name} #{object.last_name}"
end
def self.full_name__sql(scope)
"first_name || ' ' || last_name"
end
end
The scope is passed to methods in a serializer, while there are no
arguments passed to __sql
methods in a model. You will not have access
to the current_user
alias of scope
, but the scope passed in will be
the same object. Right now, this string is used as a SQL literal, so be
sure to not use untrusted values in the return value. This feature may
change before the 1.0 release, if a cleaner implementation is found.
To work on postgres_ext-serializers locally, follow these steps:
- Run
bundle install
, this will install (almost) all the development dependencies - Run
gem install byebug
(not a declared dependency to not break CI) - Run
bundle exec rake setup
, this will set up the.env
file necessary to run the tests and set up the database - Run
bundle exec rake db:create
, this will create the test database - Run
bundle exec rake db:migrate
, this will set up the database tables required by the test - Run
BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.0.x' bundle install --quiet
to create the Gemfile.lock. - Run
bundle exec rake test:all
to run tests against all supported versions of Active Record (currently only 4.0.x)
We are very thankful for the many contributors
This gem follows Semantic Versioning
Please do! We are always looking to improve this gem.
DockYard, Inc © 2014