pg-enum 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pg_enum.rb +24 -12
  3. data/test/pg_enum_test.rb +8 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66e50be4876e131e93d1399ef75a2e1249f57d9f
4
- data.tar.gz: 7136105c3614899d6a3f2d69856fee9bb657d975
3
+ metadata.gz: 7cdafa9079223a850e747a9f0001f57455671ee9
4
+ data.tar.gz: 27529331b76515a61879c0c87bc22a73bd4fa6f1
5
5
  SHA512:
6
- metadata.gz: 3bb5ddef3034ab90cfb5ec8544b81a3f640e351b8907082f3820ee2a941230a3cc8a0f776fb07295d47219a5651be72e2c9ed701403ec80c7905b359ff4c6132
7
- data.tar.gz: f686631000ac8459dfe907825950604d09be9d3cdd756b5c108f1b5e32730399106a693070b58cc39ec5c1509014770cf4b5fdbde76d49aaf889ff35cbcaf875
6
+ metadata.gz: 5558aa1310676410177a9ccd2765ed1f9de85da412b7e9fdc9e8a7b742e623a95130708d2153b0e0d72c7836e6ec6d9f0f529cbcfd2699eb968a5f482e2bf29f
7
+ data.tar.gz: 758dbcf652c14658fe9c1ec709ed542445a452762459c274b7400c3e84bd088d4b3f1a1dc4c1f27d6a69bfda1775005f9c6500693bf925b308cc104fa958a1a3
data/lib/pg_enum.rb CHANGED
@@ -1,18 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/concern"
4
+ require "active_support/lazy_load_hooks"
5
+ require "rails/railtie"
4
6
 
5
7
  module PgEnum
6
8
  extend ActiveSupport::Concern
7
9
 
8
10
  ENUM_SQL = <<~SQL.squish
9
- SELECT e.enumlabel
11
+ SELECT t.typname AS name, e.enumlabel AS value
10
12
  FROM pg_enum e
11
13
  JOIN pg_type t
12
14
  ON e.enumtypid = t.oid
13
- WHERE t.typname = ?
14
15
  SQL
15
16
 
17
+ mattr_accessor :enums_hash
18
+
19
+ def self.load_enums_hash(connection)
20
+ self.enums_hash = enum_rows(connection).each_with_object({}) do |row, hash|
21
+ hash.deep_merge!(row["name"] => { row["value"].to_sym => row["value"] })
22
+ end
23
+ end
24
+
25
+ def self.enum_rows(connection)
26
+ connection.select_all(ENUM_SQL)
27
+ end
28
+
29
+ class Railtie < Rails::Railtie
30
+ initializer "pg-enum.enums", after: "active_record.initialize_database" do
31
+ ActiveSupport.on_load(:active_record) do
32
+ PgEnum.load_enums_hash(connection)
33
+ end
34
+ end
35
+ end
36
+
16
37
  class_methods do
17
38
  def pg_enum(attr, options = {})
18
39
  column = columns_hash[attr.to_s]
@@ -21,16 +42,7 @@ module PgEnum
21
42
  raise "PgEnum: #{ model.table_name }.#{ attr } is not an enum"
22
43
  end
23
44
 
24
- enum(options.merge(column.name.to_sym => enum_values(column)))
25
- end
26
-
27
- private
28
-
29
- def enum_values(column)
30
- connection
31
- .select_all(sanitize_sql_array([ENUM_SQL, column.sql_type]))
32
- .map { |v| v["enumlabel"] }
33
- .each_with_object({}) { |v, h| h[v.to_sym] = v }
45
+ enum(options.merge(column.name.to_sym => PgEnum.enums_hash[column.sql_type]))
34
46
  end
35
47
  end
36
48
 
data/test/pg_enum_test.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "active_record"
5
+ require "rails"
5
6
  require "pg"
6
7
  require "minitest/autorun"
7
8
  require_relative "../lib/pg_enum"
@@ -32,6 +33,13 @@ ActiveRecord::Schema.define do
32
33
  end
33
34
  end
34
35
 
36
+ class Application < Rails::Application
37
+ config.eager_load = false
38
+ config.root = File.dirname(__FILE__)
39
+ end
40
+
41
+ Application.initialize!
42
+
35
43
  class Conversation < ActiveRecord::Base
36
44
  include PgEnum
37
45
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2017-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord