jsonb_accessor 1.0.0.beta.3 → 1.0.0.beta.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daa3bf1bf3902f9507356358bb715623a66f622a
4
- data.tar.gz: b6f917cb7dab2ad61077a23a2b94d8a65545b6f5
3
+ metadata.gz: 9342c69dc4817b5f014149d149fd8fb92ad650ff
4
+ data.tar.gz: d3b5f27e415c190f63b9275cc28c648fc2510023
5
5
  SHA512:
6
- metadata.gz: 1d3f263eb28a0b7f2d0b1f813cc0a9d24b2b1fd0814eca2916ca474d150eb751287e064b82464f541224a53a4b6576bffc35012a56a4ea84bf240646e5c3a288
7
- data.tar.gz: 5a41d9c6cd1b5d45b3d8f76b974493c668a28453575d0247fb896cc842edfcc898d4d3aedc6158c54e8786cdcb0b962c7bf7be942b169699951aa4695fb561c3
6
+ metadata.gz: 2b2e8d7ffbc17b5b8455c8e95f686a1ad24a2f3cbd7f6166d7bcf2300922316221248617dd021fb0d3f59db45f5b9bb4ff912540c4876f0b3a92484d688fc29a
7
+ data.tar.gz: 6e1453d01c791b69cf5ec9c0f525819bc8eed173fdbb4f914c8e190f7a5f6b7ae0ad8e966707a023299d69f2e33df1cb0af74b3ef7374053127cbe8c430a14dd
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # JSONb Accessor
2
2
 
3
- Created by [<img src="https://raw.githubusercontent.com/devmynd/jsonb_accessor/master/devmynd-logo.png" alt="DevMynd Logo" />](https://www.devmynd.com/) [![Gem Version](https://badge.fury.io/rb/jsonb_accessor.svg)](http://badge.fury.io/rb/jsonb_accessor) [![Build Status](https://travis-ci.org/devmynd/jsonb_accessor.svg)](https://travis-ci.org/devmynd/jsonb_accessor) <img src="https://raw.githubusercontent.com/devmynd/jsonb_accessor/master/json-bee.png" alt="JSONb Accessor Logo" align="right" />
3
+ Created by &nbsp;&nbsp;&nbsp; [<img src="https://raw.githubusercontent.com/devmynd/jsonb_accessor/master/devmynd-logo.png" alt="DevMynd Logo" />](https://www.devmynd.com/)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/jsonb_accessor.svg)](http://badge.fury.io/rb/jsonb_accessor) &nbsp;&nbsp;&nbsp;[![Build Status](https://travis-ci.org/devmynd/jsonb_accessor.svg)](https://travis-ci.org/devmynd/jsonb_accessor) <img src="https://raw.githubusercontent.com/devmynd/jsonb_accessor/master/json-bee.png" alt="JSONb Accessor Logo" align="right" />
4
6
 
5
7
  Adds typed `jsonb` backed fields as first class citizens to your `ActiveRecord` models. This gem is similar in spirit to [HstoreAccessor](https://github.com/devmynd/hstore_accessor), but the `jsonb` column in PostgreSQL has a few distinct advantages, mostly around nested documents and support for collections.
6
8
 
@@ -27,7 +29,7 @@ This README reflects the most recent 1.0 beta. Method names and interfaces may s
27
29
  Add this line to your application's `Gemfile`:
28
30
 
29
31
  ```ruby
30
- gem "jsonb_accessor", "1.0.0.beta.3"
32
+ gem "jsonb_accessor", "1.0.0.beta.4"
31
33
  ```
32
34
 
33
35
  And then execute:
@@ -71,7 +73,7 @@ class Product < ActiveRecord::Base
71
73
  end
72
74
  ```
73
75
 
74
- A brief note about defaults: `default` works pretty much as you would expect in practice, but it actually becomes part of a defaults hash that is the `default` value for the jsonb column (in the above example it would be `:data`).
76
+ The `default` option works pretty much as you would expect in practice; if no values are set for the attributes, a hash of the specified default values is saved to the jsonb column.
75
77
 
76
78
  You can also pass in a `store_key` option.
77
79
 
@@ -154,6 +156,14 @@ Product.all.data_where(reviewed_at: { before: Time.current }, price: { greater_t
154
156
  ```
155
157
  This scope makes use of the `jsonb_contains`, `jsonb_number_where`, and `jsonb_time_where` `scope`s.
156
158
 
159
+ ### `jsonb_where_not`
160
+
161
+ Just the opposite of `jsonb_where`. Note that this will automatically exclude all records that contain `null` in their jsonb column (the `data` column, in the example below).
162
+
163
+ ```ruby
164
+ Product.all.jsonb_where_not(:data, reviewed_at: { before: Time.current }, p: { greater_than: 5 })
165
+ ```
166
+
157
167
  ### `jsonb_contains`
158
168
 
159
169
  Returns all records that contain the given JSON paths.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- jsonb_accessor (1.0.0.beta.3)
4
+ jsonb_accessor (1.0.0.beta.4)
5
5
  activerecord (>= 5.0)
6
6
  pg (>= 0.18.1)
7
7
 
@@ -29,7 +29,7 @@ module JsonbAccessor
29
29
  names_and_defaults = field_types.each_with_object({}) do |(name, type), mapping|
30
30
  _type, options = Array(type)
31
31
  field_default = options.try(:delete, :default)
32
- mapping[name.to_s] = field_default if field_default
32
+ mapping[name.to_s] = field_default unless field_default.nil?
33
33
  end
34
34
 
35
35
  # Get store keys to default values mapping
@@ -39,9 +39,13 @@ module JsonbAccessor
39
39
  arg.keys.map(&:to_s).all? { |key| JsonbAccessor::TIME_OPERATORS.include?(key) }
40
40
  end
41
41
 
42
+ ORDER_DIRECTIONS = [:asc, :desc, "asc", "desc"].freeze
43
+
42
44
  module QueryBuilder
43
45
  extend ActiveSupport::Concern
44
46
  InvalidColumnName = Class.new(StandardError)
47
+ InvalidFieldName = Class.new(StandardError)
48
+ InvalidDirection = Class.new(StandardError)
45
49
 
46
50
  def self.validate_column_name!(query, column_name)
47
51
  if query.model.columns.none? { |column| column.name == column_name.to_s }
@@ -49,6 +53,20 @@ module JsonbAccessor
49
53
  end
50
54
  end
51
55
 
56
+ def self.validate_field_name!(query, column_name, field_name)
57
+ store_keys = query.model.public_send("jsonb_store_key_mapping_for_#{column_name}").values
58
+ if store_keys.exclude?(field_name.to_s)
59
+ valid_field_names = store_keys.map { |key| "`#{key}`" }.join(", ")
60
+ raise InvalidFieldName, "`#{field_name}` is not a valid field name, valid field names include: #{valid_field_names}"
61
+ end
62
+ end
63
+
64
+ def self.validate_direction!(option)
65
+ if ORDER_DIRECTIONS.exclude?(option)
66
+ raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted"
67
+ end
68
+ end
69
+
52
70
  def self.convert_keys_to_store_keys(attributes, store_key_mapping)
53
71
  attributes.each_with_object({}) do |(name, value), new_attributes|
54
72
  store_key = store_key_mapping[name.to_s]
@@ -126,6 +144,13 @@ module JsonbAccessor
126
144
 
127
145
  excludes_attributes.empty? ? query : query.jsonb_excludes(column_name, excludes_attributes)
128
146
  end)
147
+
148
+ scope(:jsonb_order, lambda do |column_name, field_name, direction|
149
+ JsonbAccessor::QueryBuilder.validate_column_name!(all, column_name)
150
+ JsonbAccessor::QueryBuilder.validate_field_name!(all, column_name, field_name)
151
+ JsonbAccessor::QueryBuilder.validate_direction!(direction)
152
+ order("(#{table_name}.#{column_name} -> '#{field_name}') #{direction}")
153
+ end)
129
154
  end
130
155
  end
131
156
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JsonbAccessor
3
- VERSION = "1.0.0.beta.3"
3
+ VERSION = "1.0.0.beta.4"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonb_accessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta.3
4
+ version: 1.0.0.beta.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Crismali
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2017-03-16 00:00:00.000000000 Z
13
+ date: 2017-04-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord