hario 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f50d53adcbb963fc89944af16e00690535a7c6ab
4
- data.tar.gz: 45235182715628de22cdde31fa12a65556c5fcca
3
+ metadata.gz: 879fa179f9fab01c486396fc9afd19c19b98c674
4
+ data.tar.gz: 180dd6a66f7b4fd8a63403a70a955a8f9a5da1da
5
5
  SHA512:
6
- metadata.gz: 7a2f840423cda8d11c7c702b2512646bbddbfe55022b4b173262f3fcd404b80cfd2adbbf6f0891dee7fbde5297889758b58cf6622d37eed5816d3b7036bc73a8
7
- data.tar.gz: d0017e3e65ae1f31e4cbf46d56b12092f056fcddfbda1703f059ce2b2beaa5373ed75a71abee4225ba28675e4f4d8037639fcedf691f0620afff6829ba930443
6
+ metadata.gz: 4dd7b5574c72a7157316dd8a59f5f8f809c8c12d896a563627eddf8eed6e76fc8feafb7766edd45137a7b8a468dd314649592ef2eedb3d369cfbdd41a26cbad6
7
+ data.tar.gz: e972fde3bfb04f2b42e7ff87d2958ac4d6d442da443c61aaa7907d4452712bb748a8efe0116c6583e15f4de8f266d58c59fda32f82a913c1ec3b951f63178f1d
data/README.md CHANGED
@@ -24,7 +24,7 @@ Add `include Hario::Filterable` to your AR model to add the `search` method, for
24
24
 
25
25
  ```ruby
26
26
  def Brand < ActiveRecord::Base
27
- include Hario::Filterable
27
+ extend Hario::Filterable
28
28
 
29
29
  has_many :products
30
30
  end
@@ -20,7 +20,7 @@ class FilterParser
20
20
  association_chain, attribute, operator = parse_descriptor(descriptor)
21
21
  condition = build_condition(association_chain, attribute, operator, value)
22
22
 
23
- nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
23
+ nested_associations = (association_chain.dup << {}).reverse.inject { |v, key| { key => v } }
24
24
  joins = m[0].deep_merge(nested_associations)
25
25
  wheres = m[1] + [condition]
26
26
  [joins, wheres]
@@ -24,7 +24,7 @@ class PluckParser
24
24
  ns.each do |p|
25
25
  association_chain, attribute = parse_namespace(p)
26
26
 
27
- nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
27
+ nested_associations = (association_chain.dup << {}).reverse.inject { |v, key| { key => v } }
28
28
  @join_clause.deep_merge!(nested_associations)
29
29
 
30
30
  attribute_table = table_name_from_association_chain(association_chain)
data/lib/hario/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hario
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/hario.rb CHANGED
@@ -4,33 +4,33 @@ require "hario/behaviours/pluck"
4
4
 
5
5
  module Hario
6
6
  module Filterable
7
- extend ActiveSupport::Concern
7
+ def search(filters, pluck = nil)
8
+ s = all
9
+ s = s.apply_filters(filters) if filters
10
+ s = s.apply_pluck(pluck) if pluck
11
+ s
12
+ end
8
13
 
9
- module ClassMethods
10
- def search(filters, pluck = nil)
11
- s = all
12
- s = s.apply_filters(filters) if filters
13
- s = s.apply_pluck(pluck) if pluck
14
- s
15
- end
14
+ def apply_filters(filters)
15
+ fp = FilterParser.new(filters, self)
16
16
 
17
- def apply_filters(filters)
18
- fp = FilterParser.new(filters, self)
17
+ fp.where_clauses.reduce(joins(fp.join_clause), &:where)
18
+ end
19
19
 
20
- fp.where_clauses.reduce(joins(fp.join_clause), &:where)
21
- end
20
+ def apply_pluck(pluck)
21
+ pp = PluckParser.new(pluck, self)
22
22
 
23
- def apply_pluck(pluck)
24
- pp = PluckParser.new(pluck, self)
23
+ results = joins(pp.join_clause).hash_pluck(*pp.pluck_clause)
25
24
 
26
- results = hash_pluck(*pp.pluck_clause)
25
+ remove_local_table_prefix(results)
26
+ end
27
27
 
28
- remove_local_table_prefix(results)
29
- end
28
+ def remove_local_table_prefix(results)
29
+ results.map{ |r| r.transform_keys!{ |k| k.gsub(/^#{table_name}\./, '') } }
30
+ end
30
31
 
31
- def remove_local_table_prefix(results)
32
- results.map{ |r| r.transform_keys!{ |k| k.gsub(/^#{table_name}\./, '') } }
33
- end
32
+ def hash_pluck(*keys)
33
+ pluck(*keys).map{ |vals| Hash[keys.zip(Array(vals))] }
34
34
  end
35
35
  end
36
36
  end
@@ -1,6 +1,6 @@
1
1
  require "minitest/autorun"
2
2
 
3
- class Hario::Test < Minitest::Unit::TestCase
3
+ class Hario::Test < Minitest::Test
4
4
  def teardown
5
5
  DatabaseRewinder.clean
6
6
  end
data/test/models.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  require 'hario'
2
2
 
3
3
  class Brand < ActiveRecord::Base
4
- include Hario::Filterable
4
+ extend Hario::Filterable
5
5
 
6
6
  has_many :products
7
7
  end
8
8
 
9
9
  class Product < ActiveRecord::Base
10
+ extend Hario::Filterable
11
+
10
12
  belongs_to :brand
11
13
  belongs_to :category, class_name: "ProductCategory"
12
14
  end
data/test/pluck_test.rb CHANGED
@@ -0,0 +1,19 @@
1
+ require_relative 'test_helper'
2
+
3
+ class PluckTest < Hario::Test
4
+ def test_simple_pluck
5
+ brands = Brand.search(nil, ["name"])
6
+
7
+ assert_equal ["id", "name"], brands.flat_map(&:keys).uniq,
8
+ "Pluck not returning correct attributes"
9
+ end
10
+
11
+ def test_pluck_through_association
12
+ # TODO: fix this so that it returns the association_name.attribute as
13
+ # the hash key, rather than table_name.attribute (different to input)
14
+ products = Product.search(nil, ["name", "brand.name"])
15
+
16
+ assert_equal ["id", "name", "brands.name"], products.flat_map(&:keys).uniq,
17
+ "Pluck not returning correct attributes with association pluck"
18
+ end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hario
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Campbell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-26 00:00:00.000000000 Z
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord