assorted 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c1b25bd61cd38f3d4f8bca37962bdcf283c833c
4
- data.tar.gz: cc009513f6595ba60033fefda3d7b623186adbfd
3
+ metadata.gz: e39934bb2ca6d839e3d8b1f07aab05a0eaf75396
4
+ data.tar.gz: 1fe1404fa394fb310b2e149a15253327d157e5de
5
5
  SHA512:
6
- metadata.gz: 81f2402794924d399d317d5ad99331e527648c083a64cdc70a13d231e7710f70bc51f6d37feca7aab5acc385621a070a0711511945745918d8a6a0e852cc618d
7
- data.tar.gz: 7eda1d0e7e2169655a90d79d173c5f8aa41595653c957a3f2766eb22bdde8b803860b9e40459cf0888e8706c7c190be9c23af820390b0fbe8236105c5edf591b
6
+ metadata.gz: 766d78c00956d8e60603f83c1260776a582c9edf6769dd0e0966f4ba2f95ea4de120dc4ea7d529660d04440cfae42f59914d47c81fa163bc7fc86294a883d0a0
7
+ data.tar.gz: bbf7007d6bde7097577c34e297d1357b099de1a5852a8567c2965ebe4a50d18f02aff5225e7161f28b8e8a25e3d09ccc84fdf2328d0a81dd6ff6d2e770d4bd00
@@ -3,6 +3,14 @@ require "active_record"
3
3
  require "assorted/version"
4
4
  require "assorted/scopes"
5
5
 
6
+ module Assorted
7
+ def self.options
8
+ @options ||= {
9
+ default_sort_column: :created_at,
10
+ }
11
+ end
12
+ end
13
+
6
14
  ActiveSupport.on_load(:active_record) do
7
15
  extend Assorted::Scopes
8
16
  end
@@ -8,8 +8,8 @@ module Assorted
8
8
  sanitized_order(column, :desc)
9
9
  end
10
10
 
11
- def set_sorting_column(column)
12
- self.sorting_column = column
11
+ def assorted(options)
12
+ assorted_options.merge!(options)
13
13
  end
14
14
 
15
15
  private
@@ -23,11 +23,11 @@ module Assorted
23
23
  end
24
24
 
25
25
  def sorting_column
26
- @sorting_column ||= :created_at
26
+ assorted_options[:default_sort_column] || Assorted.options[:default_sort_column]
27
27
  end
28
28
 
29
- def sorting_column=(column)
30
- @sorting_column = column
29
+ def assorted_options
30
+ @assorted_options ||= {}
31
31
  end
32
32
 
33
33
  end
@@ -1,3 +1,3 @@
1
1
  module Assorted
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -18,7 +18,7 @@ RSpec.describe Assorted::Scopes do
18
18
  end
19
19
 
20
20
  it "prevents SQL injection attacks" do
21
- record = ExampleRecord.create
21
+ ExampleRecord.create
22
22
  injection_attempt = "created_at desc; delete * from example_records;"
23
23
 
24
24
  expect { ExampleRecord.asc(injection_attempt) }.to raise_exception(ActiveRecord::StatementInvalid)
@@ -30,7 +30,7 @@ RSpec.describe Assorted::Scopes do
30
30
  less = ExampleRecord.create(example_count: 1)
31
31
 
32
32
  class ExampleRecord
33
- set_sorting_column :example_count
33
+ assorted default_sort_column: :example_count
34
34
  end
35
35
 
36
36
  expect(ExampleRecord.asc).to eq([less, more])
@@ -4,4 +4,17 @@ RSpec.describe Assorted do
4
4
  it "includes itself in ActiveRecord::Base" do
5
5
  expect(ActiveRecord::Base.ancestors).to include(Assorted::Scopes)
6
6
  end
7
+
8
+ describe "#options" do
9
+ it "defaults to sort with created_at" do
10
+ expect(Assorted.options[:default_sort_column]).to eq :created_at
11
+ end
12
+
13
+ it "remembers the new options given to it" do
14
+ Assorted.options[:foo] = :bar
15
+
16
+ expect(Assorted.options[:foo]).to eq(:bar)
17
+ end
18
+ end
19
+
7
20
  end
@@ -13,11 +13,10 @@ RSpec.configure do |config|
13
13
  table.timestamps
14
14
  end
15
15
 
16
- original_sorting_column = ExampleRecord.send(:sorting_column)
17
-
18
16
  example.run
19
17
 
20
- ExampleRecord.send(:sorting_column=, original_sorting_column)
18
+ ExampleRecord.instance_variable_set(:@assorted_options, nil)
19
+ Assorted.instance_variable_set(:@options, nil)
21
20
 
22
21
  raise ActiveRecord::Rollback
23
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assorted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Byrne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-12 00:00:00.000000000 Z
11
+ date: 2014-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord