GitXplorerGitXplorer
d

activerecord_pg_stuff

public
2 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
cc97a465353878c3f747b7bfe16ab117bca099b7

bump version to 0.2.1

ddmexe committed 6 years ago
Verified
db5c719d687926b63f54a22299d021f62586f467

Merge pull request #2 from envoy/appraisals

ddmexe committed 6 years ago
Unverified
be02d7d0513f2f2fc4832f4174926a62e2236de7

Add gemfile to the matrix

kkamal committed 6 years ago
Unverified
9ba6a347b393a89d2e052568c7e2841712169d40

Fix incompatibility with AR 5.1

kkamal committed 6 years ago
Unverified
b5dab6697c0dd2039ce2b4f081e2cd54969a8ab9

Add Appraisals for AR 5.1 and 5.2

kkamal committed 6 years ago
Unverified
4328957078c5efe106a9d59a9a5b14f9a7b07d39

Add appraisal gem

kkamal committed 6 years ago

README

The README file for this repository.

ActiveRecordPgStuff

Adds support for working with temporary tables and pivot tables (PostgreSQL only).

  • Build Status

Installation

Add this line to your application's Gemfile:

gem 'activerecord_pg_stuff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord_pg_stuff

Usage

Temporary tables

Example:

User.select(:id, :email).temporary_table do |rel|
  rel.pluck(:id)
end

Pivot tables

Before using, you need to create the extension:

CREATE EXTENSION tablefunc

Example:

CREATE TABLE payments (id integer, amount integer, seller_id integer, created_at timestamp)
INSERT INTO payments
  VALUES (1, 1,  1, '2012-10-12 10:00 UTC'),
         (2, 3,  1, '2012-11-12 10:00 UTC'),
         (3, 5,  2, '2012-09-12 10:00 UTC'),
         (4, 7,  2, '2012-10-12 10:00 UTC'),
         (5, 11, 2, '2012-11-12 10:00 UTC'),
         (6, 13, 2, '2012-11-12 10:00 UTC')

Payment
  .select("SUM(amount) AS amount", :seller_id, "DATE_TRUNC('month', created_at) AS month")
  .group("seller_id, DATE_TRUNC('month', created_at)")
  .temporary_table do |rel|
    # :month     - for row
    # :seller_id - for column
    # :amount    - for value
    rel.pivot(:month, :seller_id, :amount)
end

expect(result.headers).to eq [nil, 1, 2]

expect(result.rows).to eq [
  [ Time.utc(2012, 9,  1), nil, 5  ],
  [ Time.utc(2012, 10, 1), 1,   7  ],
  [ Time.utc(2012, 11, 1), 3,   24 ],
]

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request