assorted 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/assorted/scopes.rb +29 -0
- data/lib/assorted/version.rb +3 -0
- data/lib/assorted.rb +8 -0
- data/spec/lib/assorted/scopes_spec.rb +37 -0
- data/spec/lib/assorted_spec.rb +7 -0
- data/spec/spec_helper.rb +70 -0
- data/spec/support/active_record.rb +31 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 91e2d41bfce1a1dc7df96c83a9b5d5baa6fc939f
|
4
|
+
data.tar.gz: 71b2621335edd05c40d5ddd7c7c5ab96760aa916
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90c0b02d1554a2ca14b35f82eb329dcc6af33672c31e93616406546dfaefcb9a5bfec7b692c64e54b364131a594243ffbc4f24b263825eac7e616349acec3047
|
7
|
+
data.tar.gz: a213cf2f5f0ce389298e54daef8f47ffe5f2433f6eddd1cf49e325f213fbc7e5b71a53b1af3609b4363a3b5faa4abbd956e897c2805d5396baa66197c65f3fec
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Assorted
|
2
|
+
module Scopes
|
3
|
+
def asc(column = sorting_column)
|
4
|
+
sanitized_order(column, :asc)
|
5
|
+
end
|
6
|
+
|
7
|
+
def desc(column = sorting_column)
|
8
|
+
sanitized_order(column, :desc)
|
9
|
+
end
|
10
|
+
|
11
|
+
def sorting_column
|
12
|
+
@sorting_column ||= :created_at
|
13
|
+
end
|
14
|
+
|
15
|
+
def sorting_column=(column)
|
16
|
+
@sorting_column = column
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def sanitized_order(column, direction)
|
22
|
+
if attribute_names.include?(column.to_s)
|
23
|
+
order("#{table_name}.#{column} #{direction}")
|
24
|
+
else
|
25
|
+
raise ActiveRecord::StatementInvalid, "Unknown column #{column}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/assorted.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe Assorted::Scopes do
|
4
|
+
it "sorts by created_at by default" do
|
5
|
+
first = ExampleRecord.create(created_at: 2.days.ago)
|
6
|
+
second = ExampleRecord.create(created_at: 1.day.ago)
|
7
|
+
|
8
|
+
expect(ExampleRecord.asc).to eq([first, second])
|
9
|
+
expect(ExampleRecord.desc).to eq([second, first])
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sorts by the given column, if provided" do
|
13
|
+
more = ExampleRecord.create(example_count: 2)
|
14
|
+
less = ExampleRecord.create(example_count: 1)
|
15
|
+
|
16
|
+
expect(ExampleRecord.asc(:example_count)).to eq([less, more])
|
17
|
+
expect(ExampleRecord.desc(:example_count)).to eq([more, less])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "prevents SQL injection attacks" do
|
21
|
+
record = ExampleRecord.create
|
22
|
+
injection_attempt = "created_at desc; delete * from example_records;"
|
23
|
+
|
24
|
+
expect { ExampleRecord.asc(injection_attempt) }.to raise_exception(ActiveRecord::StatementInvalid)
|
25
|
+
expect(ExampleRecord.count).to eq(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "allows an alternate default sorting column to be supplied" do
|
29
|
+
more = ExampleRecord.create(example_count: 2)
|
30
|
+
less = ExampleRecord.create(example_count: 1)
|
31
|
+
|
32
|
+
ExampleRecord.sorting_column = :example_count
|
33
|
+
|
34
|
+
expect(ExampleRecord.asc).to eq([less, more])
|
35
|
+
expect(ExampleRecord.desc).to eq([more, less])
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require "assorted"
|
2
|
+
|
3
|
+
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each do |file|
|
4
|
+
require file
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.expect_with :rspec do |expectations|
|
9
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
10
|
+
# and `failure_message` of custom matchers include text for helper methods
|
11
|
+
# defined using `chain`, e.g.:
|
12
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
13
|
+
# # => "be bigger than 2 and smaller than 4"
|
14
|
+
# ...rather than:
|
15
|
+
# # => "be bigger than 2"
|
16
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
17
|
+
end
|
18
|
+
|
19
|
+
config.mock_with :rspec do |mocks|
|
20
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
21
|
+
# a real object. This is generally recommended, and will default to
|
22
|
+
# `true` in RSpec 4.
|
23
|
+
mocks.verify_partial_doubles = true
|
24
|
+
end
|
25
|
+
|
26
|
+
# These two settings work together to allow you to limit a spec run
|
27
|
+
# to individual examples or groups you care about by tagging them with
|
28
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
29
|
+
# get run.
|
30
|
+
config.filter_run :focus
|
31
|
+
config.run_all_when_everything_filtered = true
|
32
|
+
|
33
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
34
|
+
# For more details, see:
|
35
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
36
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
37
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
38
|
+
config.disable_monkey_patching!
|
39
|
+
|
40
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
41
|
+
# be too noisy due to issues in dependencies.
|
42
|
+
config.warnings = true
|
43
|
+
|
44
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
45
|
+
# file, and it's useful to allow more verbose output when running an
|
46
|
+
# individual spec file.
|
47
|
+
if config.files_to_run.one?
|
48
|
+
# Use the documentation formatter for detailed output,
|
49
|
+
# unless a formatter has already been configured
|
50
|
+
# (e.g. via a command-line flag).
|
51
|
+
config.default_formatter = 'doc'
|
52
|
+
end
|
53
|
+
|
54
|
+
# Print the 5 slowest examples and example groups at the
|
55
|
+
# end of the spec run, to help surface which specs are running
|
56
|
+
# particularly slow.
|
57
|
+
config.profile_examples = 5
|
58
|
+
|
59
|
+
# Run specs in random order to surface order dependencies. If you find an
|
60
|
+
# order dependency and want to debug it, you can fix the order by providing
|
61
|
+
# the seed, which is printed after each run.
|
62
|
+
# --seed 1234
|
63
|
+
config.order = :random
|
64
|
+
|
65
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
66
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
67
|
+
# test failures related to randomization by passing the same `--seed` value
|
68
|
+
# as the one that triggered the failure.
|
69
|
+
Kernel.srand config.seed
|
70
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "spec/test.db")
|
2
|
+
|
3
|
+
class ExampleRecord < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.around do |example|
|
8
|
+
ActiveRecord::Base.transaction do
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
ActiveRecord::Migration.create_table(:example_records) do |table|
|
11
|
+
table.integer :user_id
|
12
|
+
table.integer :example_count
|
13
|
+
table.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
original_sorting_column = ExampleRecord.sorting_column
|
17
|
+
|
18
|
+
example.run
|
19
|
+
|
20
|
+
ExampleRecord.sorting_column = original_sorting_column
|
21
|
+
|
22
|
+
raise ActiveRecord::Rollback
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
config.after(:suite) do
|
27
|
+
ActiveRecord::Base.connection.instance_variable_get("@config").tap do |configuration|
|
28
|
+
File.delete(configuration[:database])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assorted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Byrne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- code@patrickbyrne.net
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- lib/assorted.rb
|
105
|
+
- lib/assorted/scopes.rb
|
106
|
+
- lib/assorted/version.rb
|
107
|
+
- spec/lib/assorted/scopes_spec.rb
|
108
|
+
- spec/lib/assorted_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/support/active_record.rb
|
111
|
+
homepage: https://github.com/dribbble/assorted
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Add sorting scopes `asc` and `desc` to your ActiveRecord models.
|
135
|
+
test_files:
|
136
|
+
- spec/lib/assorted/scopes_spec.rb
|
137
|
+
- spec/lib/assorted_spec.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
- spec/support/active_record.rb
|