trailblazer-finder 0.1.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
- data/.rubocop.yml +3 -43
- data/.rubocop_todo.yml +42 -196
- data/.travis.yml +5 -8
- data/Gemfile +2 -2
- data/LICENSE.txt +7 -21
- data/README.md +177 -292
- data/Rakefile +12 -12
- data/lib/trailblazer/finder.rb +48 -23
- data/lib/trailblazer/finder/activity/find.rb +57 -0
- data/lib/trailblazer/finder/activity/prepare.rb +29 -0
- data/lib/trailblazer/finder/activity/prepare/adapters.rb +66 -0
- data/lib/trailblazer/finder/activity/prepare/entity.rb +32 -0
- data/lib/trailblazer/finder/activity/prepare/filters.rb +36 -0
- data/lib/trailblazer/finder/activity/prepare/paging.rb +49 -0
- data/lib/trailblazer/finder/activity/prepare/params.rb +33 -0
- data/lib/trailblazer/finder/activity/prepare/properties.rb +47 -0
- data/lib/trailblazer/finder/activity/prepare/sorting.rb +57 -0
- data/lib/trailblazer/finder/activity/process.rb +18 -0
- data/lib/trailblazer/finder/activity/process/adapters.rb +48 -0
- data/lib/trailblazer/finder/activity/process/filters.rb +28 -0
- data/lib/trailblazer/finder/activity/process/paging.rb +26 -0
- data/lib/trailblazer/finder/activity/process/predicates.rb +30 -0
- data/lib/trailblazer/finder/activity/process/sorting.rb +26 -0
- data/lib/trailblazer/finder/adapters/active_record/paging.rb +9 -8
- data/lib/trailblazer/finder/adapters/active_record/predicates.rb +78 -17
- data/lib/trailblazer/finder/adapters/active_record/sorting.rb +14 -12
- data/lib/trailblazer/finder/adapters/basic/paging.rb +21 -0
- data/lib/trailblazer/finder/adapters/basic/predicates.rb +115 -0
- data/lib/trailblazer/finder/adapters/basic/sorting.rb +40 -0
- data/lib/trailblazer/finder/adapters/kaminari/paging.rb +21 -0
- data/lib/trailblazer/finder/adapters/sequel/paging.rb +9 -8
- data/lib/trailblazer/finder/adapters/sequel/predicates.rb +78 -17
- data/lib/trailblazer/finder/adapters/sequel/sorting.rb +17 -15
- data/lib/trailblazer/finder/adapters/will_paginate/paging.rb +21 -0
- data/lib/trailblazer/finder/base.rb +20 -83
- data/lib/trailblazer/finder/dsl.rb +56 -0
- data/lib/trailblazer/finder/find.rb +28 -15
- data/lib/trailblazer/finder/helpers/basic.rb +44 -0
- data/lib/trailblazer/finder/helpers/sorting.rb +54 -0
- data/lib/trailblazer/finder/utils/array.rb +20 -0
- data/lib/trailblazer/finder/utils/extra.rb +10 -20
- data/lib/trailblazer/finder/utils/hash.rb +36 -0
- data/lib/trailblazer/finder/utils/splitter.rb +16 -17
- data/lib/trailblazer/finder/utils/string.rb +20 -10
- data/lib/trailblazer/finder/version.rb +3 -1
- data/lib/trailblazer/operation/finder.rb +20 -19
- data/spec/spec_helper.rb +5 -11
- data/spec/spec_helper_active_record.rb +11 -12
- data/spec/spec_helper_sequel.rb +4 -4
- data/spec/spec_helper_will_paginate.rb +13 -0
- data/spec/trailblazer/finder/adapters/active_record_spec.rb +267 -0
- data/spec/trailblazer/finder/adapters/basic_spec.rb +251 -0
- data/spec/trailblazer/finder/adapters/kaminari_spec.rb +78 -32
- data/spec/trailblazer/finder/adapters/sequel_spec.rb +267 -0
- data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +81 -40
- data/spec/trailblazer/finder/base_spec.rb +292 -204
- data/spec/trailblazer/finder/dsl_spec.rb +213 -0
- data/spec/trailblazer/finder/utils/hash_spec.rb +26 -0
- data/spec/trailblazer/finder/utils/splitter_spec.rb +39 -0
- data/spec/trailblazer/finder/utils/string_spec.rb +52 -9
- data/spec/trailblazer/operation/finder_spec.rb +35 -32
- data/trailblazer-finder.gemspec +31 -30
- metadata +119 -137
- data/lib/trailblazer/finder/adapters.rb +0 -26
- data/lib/trailblazer/finder/adapters/active_record.rb +0 -32
- data/lib/trailblazer/finder/adapters/data_mapper.rb +0 -32
- data/lib/trailblazer/finder/adapters/data_mapper/paging.rb +0 -20
- data/lib/trailblazer/finder/adapters/data_mapper/predicates.rb +0 -42
- data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +0 -38
- data/lib/trailblazer/finder/adapters/friendly_id.rb +0 -31
- data/lib/trailblazer/finder/adapters/kaminari.rb +0 -18
- data/lib/trailblazer/finder/adapters/sequel.rb +0 -32
- data/lib/trailblazer/finder/adapters/will_paginate.rb +0 -18
- data/lib/trailblazer/finder/errors/block_ignored.rb +0 -11
- data/lib/trailblazer/finder/errors/invalid_defined_by_value.rb +0 -11
- data/lib/trailblazer/finder/errors/invalid_number.rb +0 -16
- data/lib/trailblazer/finder/errors/missing_entity_type.rb +0 -11
- data/lib/trailblazer/finder/errors/with_ignored.rb +0 -11
- data/lib/trailblazer/finder/features.rb +0 -23
- data/lib/trailblazer/finder/features/paging.rb +0 -55
- data/lib/trailblazer/finder/features/predicate.rb +0 -32
- data/lib/trailblazer/finder/features/sorting.rb +0 -108
- data/lib/trailblazer/finder/filter.rb +0 -66
- data/lib/trailblazer/finder/predicates.rb +0 -39
- data/lib/trailblazer/finder/utils/deep_locate.rb +0 -30
- data/lib/trailblazer/finder/utils/params.rb +0 -28
- data/lib/trailblazer/finder/utils/parse.rb +0 -25
- data/spec/spec_helper_data_mapper.rb +0 -35
- data/spec/support/paging_shared_example.rb +0 -65
- data/spec/support/sorting_shared_example.rb +0 -125
- data/spec/trailblazer/finder/adapters/active_record/base_spec.rb +0 -112
- data/spec/trailblazer/finder/adapters/active_record/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +0 -101
- data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +0 -99
- data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +0 -112
- data/spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb +0 -100
- data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +0 -97
- data/spec/trailblazer/finder/adapters/friendly_id_spec.rb +0 -46
- data/spec/trailblazer/finder/adapters/sequel/base_spec.rb +0 -125
- data/spec/trailblazer/finder/adapters/sequel/paging_spec.rb +0 -64
- data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +0 -89
- data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +0 -110
- data/spec/trailblazer/finder/adapters_spec.rb +0 -110
- data/spec/trailblazer/finder/features/paging_spec.rb +0 -104
- data/spec/trailblazer/finder/features/predicates_spec.rb +0 -99
- data/spec/trailblazer/finder/features/sorting_spec.rb +0 -96
- data/spec/trailblazer/finder/features_spec.rb +0 -55
- data/spec/trailblazer/finder/filter_spec.rb +0 -133
- data/spec/trailblazer/finder/find_spec.rb +0 -72
- data/spec/trailblazer/finder/utils/extra_spec.rb +0 -41
- data/spec/trailblazer/finder/utils/params_spec.rb +0 -39
- data/spec/trailblazer/finder/utils/parse_spec.rb +0 -33
- data/spec/trailblazer/operation/paging_spec.rb +0 -68
- data/spec/trailblazer/operation/predicates_spec.rb +0 -127
- data/spec/trailblazer/operation/sorting_spec.rb +0 -80
- data/spec/trailblazer/test_spec.rb +0 -41
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trailblazer
|
4
|
+
class Finder
|
5
|
+
module Dsl
|
6
|
+
attr_reader :config
|
7
|
+
|
8
|
+
def inherited(base)
|
9
|
+
base.instance_variable_set "@config", apply_config({})
|
10
|
+
end
|
11
|
+
|
12
|
+
def entity(&block)
|
13
|
+
config[:entity] = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def paging(options, **)
|
17
|
+
config[:paging][:per_page] = options[:per_page] || 25
|
18
|
+
config[:paging][:min_per_page] = options[:min_per_page] || 10
|
19
|
+
config[:paging][:max_per_page] = options[:max_per_page] || 100
|
20
|
+
end
|
21
|
+
|
22
|
+
def property(name, options = {})
|
23
|
+
config[:properties][name] = options
|
24
|
+
config[:properties][name][:type] = options[:type] || Types::String
|
25
|
+
config[:sorting][name] = options[:sort_direction] || :desc if options[:sortable]
|
26
|
+
end
|
27
|
+
|
28
|
+
def filter_by(name, options = {}, &block)
|
29
|
+
config[:filters][name] = {}
|
30
|
+
config[:filters][name][:name] = name
|
31
|
+
config[:filters][name][:with] = options[:with] if options.include?(:with)
|
32
|
+
config[:filters][name][:block] = block || nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def adapters(*adapters)
|
36
|
+
adapters.each do |adapter|
|
37
|
+
config[:adapters].push adapter.to_s.split("::").last.gsub("}>", "") if config[:adapters]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def apply_config(options, **)
|
42
|
+
return @config = options unless options.empty?
|
43
|
+
|
44
|
+
@config = {
|
45
|
+
actions: {},
|
46
|
+
entity: nil,
|
47
|
+
properties: {},
|
48
|
+
filters: {},
|
49
|
+
paging: {},
|
50
|
+
sorting: {},
|
51
|
+
adapters: []
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -1,28 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Trailblazer
|
2
4
|
class Finder
|
3
5
|
class Find
|
4
|
-
attr_reader :params
|
6
|
+
attr_reader :params, :paging, :sorting, :filters, :config
|
5
7
|
|
6
|
-
def initialize(
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@params
|
8
|
+
def initialize(entity, params, filters, paging = nil, sorting = nil, config = nil)
|
9
|
+
@entity = entity
|
10
|
+
@filters = filters
|
11
|
+
@params = params
|
12
|
+
@paging = paging || {}
|
13
|
+
@sorting = sorting || {}
|
14
|
+
@config = config || {}
|
10
15
|
end
|
11
16
|
|
12
|
-
def
|
13
|
-
@params
|
17
|
+
def process_filters(ctx)
|
18
|
+
@params.reduce(@entity) do |entity, (name, value)|
|
19
|
+
value = Utils::String.to_date(value) if Utils::String.date?(value)
|
20
|
+
new_entity = ctx.instance_exec entity, @filters[name.to_sym][:name], value, &@filters[name.to_sym][:handler]
|
21
|
+
new_entity || entity
|
22
|
+
end
|
14
23
|
end
|
15
24
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
new_entity_type = context.instance_exec entity_type, value, &@actions[name]
|
20
|
-
new_entity_type || entity_type
|
21
|
-
end
|
25
|
+
def process_paging(ctx)
|
26
|
+
# ctx.instance_exec @paging[:current_page], @paging[:per_page], (process_filters ctx), &@paging[:handler]
|
27
|
+
ctx.instance_exec @paging[:current_page], @paging[:per_page], (@sorting.empty? ? (process_filters ctx) : (process_sorting ctx)), &@paging[:handler]
|
22
28
|
end
|
23
29
|
|
24
|
-
def
|
25
|
-
|
30
|
+
def process_sorting(ctx)
|
31
|
+
ctx.instance_exec @sorting, (process_filters ctx), &@sorting[:handler]
|
32
|
+
end
|
33
|
+
|
34
|
+
def query(ctx)
|
35
|
+
return process_paging ctx unless @paging.empty? || @paging.nil?
|
36
|
+
return process_sorting ctx unless @sorting.empty? || @sorting.nil?
|
37
|
+
|
38
|
+
process_filters ctx
|
26
39
|
end
|
27
40
|
end
|
28
41
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trailblazer
|
4
|
+
class Finder
|
5
|
+
module Helpers
|
6
|
+
module Basic
|
7
|
+
def paging
|
8
|
+
return if @errors.any?
|
9
|
+
return if @find.paging.empty?
|
10
|
+
|
11
|
+
result = Utils::Hash.remove_keys_from_hash(@find.paging, %i[handler max_per_page min_per_page])
|
12
|
+
result[:page] = result[:page] || result.delete(:current_page) || result[:current_page]
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
def params
|
17
|
+
return @options[:params] if @errors.any?
|
18
|
+
|
19
|
+
@params ||= (paging.merge @find.params).merge sort: sorting
|
20
|
+
end
|
21
|
+
|
22
|
+
def filters
|
23
|
+
@filters ||= @find.filters if @errors.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def result
|
27
|
+
@result ||= @errors.empty? ? fetch_result : {errors: @errors} if respond_to?(:fetch_result)
|
28
|
+
end
|
29
|
+
|
30
|
+
def result?
|
31
|
+
return false if @errors.any?
|
32
|
+
|
33
|
+
result.any?
|
34
|
+
end
|
35
|
+
|
36
|
+
def count
|
37
|
+
return if @errors.any?
|
38
|
+
|
39
|
+
@count ||= result.size
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trailblazer
|
4
|
+
class Finder
|
5
|
+
module Helpers
|
6
|
+
module Sorting
|
7
|
+
def sorting
|
8
|
+
return if @errors.any?
|
9
|
+
return if @find.sorting.empty?
|
10
|
+
|
11
|
+
@sorting ||= Utils::Hash.remove_keys_from_hash(@find.sorting, [:handler]).map { |r| r.join(" ") }.join(", ")
|
12
|
+
end
|
13
|
+
|
14
|
+
def sort?(attribute)
|
15
|
+
sorting.include?(attribute.to_s)
|
16
|
+
end
|
17
|
+
|
18
|
+
def sort_direction_for(attribute)
|
19
|
+
return "asc" if (!sorting.nil? && sorting.include?("#{attribute} asc")) || @find.config[:sorting][attribute.to_sym] == :asc
|
20
|
+
|
21
|
+
"desc"
|
22
|
+
end
|
23
|
+
|
24
|
+
def reverse_sort_direction_for(attribute)
|
25
|
+
return "desc" if (!sorting.nil? && sorting.include?("#{attribute} asc")) || @find.config[:sorting][attribute.to_sym] == :asc
|
26
|
+
|
27
|
+
"asc"
|
28
|
+
end
|
29
|
+
|
30
|
+
def sort_params_for(attribute)
|
31
|
+
if sorting.nil?
|
32
|
+
params.merge! sort: "#{attribute} #{sort_direction_for(attribute)}"
|
33
|
+
elsif sorting.include?(attribute.to_s)
|
34
|
+
params.merge! sort: sorting.gsub(/#{attribute} #{sort_direction_for(attribute)}/, "#{attribute} #{reverse_sort_direction_for(attribute)}")
|
35
|
+
else
|
36
|
+
params.merge! sort: "#{sorting}, #{attribute} #{sort_direction_for(attribute)}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_sort_params_for(attribute)
|
41
|
+
return unless sorting.include?(attribute.to_s)
|
42
|
+
|
43
|
+
sort = sorting.gsub(/#{attribute} #{sort_direction_for(attribute)}/, "").split(",")
|
44
|
+
sort.delete_if(&:blank?)
|
45
|
+
params.merge! sort: sort.join(",")
|
46
|
+
end
|
47
|
+
|
48
|
+
def new_sort_params_for(attribute)
|
49
|
+
params.merge! sort: "#{attribute} #{sort_direction_for(attribute)}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trailblazer
|
4
|
+
class Finder
|
5
|
+
# Helper module
|
6
|
+
module Utils
|
7
|
+
module Array
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def convert_hashes_in_array_to_struct(hash)
|
11
|
+
result = []
|
12
|
+
hash.each do |h|
|
13
|
+
result << (OpenStruct.new h)
|
14
|
+
end
|
15
|
+
result
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,28 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Trailblazer
|
2
4
|
class Finder
|
3
5
|
# Helper module
|
4
6
|
module Utils
|
5
|
-
|
6
|
-
|
7
|
-
if collection.include? item
|
8
|
-
item
|
9
|
-
else
|
10
|
-
collection.first
|
11
|
-
end
|
12
|
-
end
|
7
|
+
module Extra
|
8
|
+
module_function
|
13
9
|
|
14
|
-
def
|
15
|
-
case
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
result[key] = deep_copy(value)
|
21
|
-
end
|
22
|
-
when NilClass, FalseClass, TrueClass, Symbol, Method, Numeric
|
23
|
-
object
|
24
|
-
else
|
25
|
-
object.dup
|
10
|
+
def apply_handler(handler, predicate_handler = "Trailblazer::Finder::Adapters::Basic::Predicates")
|
11
|
+
case handler
|
12
|
+
when Symbol then ->(entity, attribute, value) { method(handler).call entity, attribute, value }
|
13
|
+
when Proc then handler
|
14
|
+
else
|
15
|
+
Object.const_get(predicate_handler).__send__ :set_eq_handler
|
26
16
|
end
|
27
17
|
end
|
28
18
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trailblazer
|
4
|
+
class Finder
|
5
|
+
# Helper module
|
6
|
+
module Utils
|
7
|
+
module Hash
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def deep_locate(comparator, object, result = [])
|
11
|
+
if object.is_a?(::Enumerable) && comparator.is_a?(::Proc)
|
12
|
+
result.push object if object.any? { |value| match_comparator?(value, comparator, object) }
|
13
|
+
(object.respond_to?(:values) ? object.values : object.entries).each do |value|
|
14
|
+
deep_locate(comparator, value, result)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
def match_comparator?(value, comparator, object)
|
22
|
+
key = nil
|
23
|
+
key, value = value if object.is_a?(::Hash)
|
24
|
+
comparator.call(key, value, object)
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_keys_from_hash(hash, keys)
|
28
|
+
hash.each do |key, _value|
|
29
|
+
hash.delete(key) if keys.include?(key)
|
30
|
+
end
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,24 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Trailblazer
|
2
4
|
class Finder
|
3
5
|
# Helper module
|
4
6
|
module Utils
|
5
7
|
class Splitter
|
6
|
-
def initialize( key, value )
|
7
|
-
@key, @value = key, value
|
8
|
-
end
|
9
|
-
|
10
8
|
attr_reader :key, :value
|
11
9
|
|
10
|
+
def initialize(key, value)
|
11
|
+
@key = key
|
12
|
+
@value = value.to_s
|
13
|
+
end
|
14
|
+
|
12
15
|
# split suffix from the key and store the two values as name and op
|
13
16
|
# return truthy if successful
|
14
|
-
def split_key(
|
17
|
+
def split_key(suffix)
|
15
18
|
rv = @key =~ /\A(?:(.*?)_)?(#{suffix})\z/
|
16
|
-
@field
|
17
|
-
|
19
|
+
@field = Regexp.last_match(1)
|
20
|
+
@predicate = Regexp.last_match(2)
|
21
|
+
!rv.nil?
|
18
22
|
end
|
19
23
|
|
20
|
-
|
21
|
-
|
24
|
+
alias === split_key
|
25
|
+
alias =~ split_key
|
22
26
|
|
23
27
|
# return name if the split was successful, or fall back to key
|
24
28
|
# which is handy when none of the predicates match and so key
|
@@ -27,14 +31,9 @@ module Trailblazer
|
|
27
31
|
(@field || @key)
|
28
32
|
end
|
29
33
|
|
30
|
-
# the
|
31
|
-
def
|
32
|
-
@
|
33
|
-
end
|
34
|
-
|
35
|
-
# the value
|
36
|
-
def value
|
37
|
-
@value.to_s
|
34
|
+
# the predicate
|
35
|
+
def predicate
|
36
|
+
@predicate.to_sym
|
38
37
|
end
|
39
38
|
end
|
40
39
|
end
|
@@ -1,18 +1,19 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Trailblazer
|
4
4
|
class Finder
|
5
5
|
# Helper module
|
6
6
|
module Utils
|
7
7
|
class String
|
8
|
-
def self.blank?(
|
9
|
-
return false if
|
10
|
-
|
8
|
+
def self.blank?(string)
|
9
|
+
return false if numeric?(string)
|
10
|
+
|
11
|
+
string.nil? || string.strip.empty?
|
11
12
|
end
|
12
13
|
|
13
|
-
def self.
|
14
|
-
|
15
|
-
rescue
|
14
|
+
def self.numeric?(string)
|
15
|
+
!Float(string).nil?
|
16
|
+
rescue StandardError
|
16
17
|
false
|
17
18
|
end
|
18
19
|
|
@@ -22,13 +23,22 @@ module Trailblazer
|
|
22
23
|
|
23
24
|
def self.underscore(text)
|
24
25
|
text
|
25
|
-
.to_s.gsub(/::/,
|
26
|
+
.to_s.gsub(/::/, "/")
|
26
27
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
27
28
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
28
|
-
.tr(
|
29
|
-
.tr(
|
29
|
+
.tr("-", "_")
|
30
|
+
.tr(" ", "_")
|
30
31
|
.downcase
|
31
32
|
end
|
33
|
+
|
34
|
+
def self.to_date(value)
|
35
|
+
Date.parse(value).strftime("%Y-%m-%d") if date?(value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.date?(date)
|
39
|
+
date_hash = Date._parse(date.to_s)
|
40
|
+
Date.valid_date?(date_hash[:year].to_i, date_hash[:mon].to_i, date_hash[:mday].to_i)
|
41
|
+
end
|
32
42
|
end
|
33
43
|
end
|
34
44
|
end
|
@@ -1,19 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
# Really gotta clean this up, but can't be bothered right now
|
3
4
|
module Trailblazer
|
4
5
|
class Operation
|
5
|
-
def self.Finder(finder_class, action = nil,
|
6
|
-
task = Trailblazer::Activity::TaskBuilder::Binary
|
6
|
+
def self.Finder(finder_class, action = nil, entity = nil)
|
7
|
+
task = Trailblazer::Activity::TaskBuilder::Binary(Finder.new)
|
7
8
|
|
8
9
|
extension = Trailblazer::Activity::TaskWrap::Merge.new(
|
9
10
|
Wrap::Inject::Defaults(
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
"finder.class" => finder_class,
|
12
|
+
"finder.entity" => entity,
|
13
|
+
"finder.action" => action
|
13
14
|
)
|
14
15
|
)
|
15
16
|
|
16
|
-
{
|
17
|
+
{task: task, id: "finder.build", Trailblazer::Activity::DSL::Extension.new(extension) => true}
|
17
18
|
end
|
18
19
|
|
19
20
|
class Finder
|
@@ -21,40 +22,40 @@ module Trailblazer
|
|
21
22
|
builder = Finder::Builder.new
|
22
23
|
options[:finder] = finder = builder.call(options, params)
|
23
24
|
options[:model] = finder # Don't like it, but somehow it's needed if contracts are loaded
|
24
|
-
options[
|
25
|
+
options["result.finder"] = result = Result.new(!finder.nil?, {})
|
25
26
|
|
26
27
|
result.success?
|
27
28
|
end
|
28
29
|
|
29
30
|
class Builder
|
30
31
|
def call(options, params)
|
31
|
-
finder_class = options[
|
32
|
-
|
33
|
-
action = options[
|
32
|
+
finder_class = options["finder.class"]
|
33
|
+
entity = options["finder.entity"] || nil
|
34
|
+
action = options["finder.action"] || :all
|
34
35
|
action = :all unless %i[all single].include?(action)
|
35
36
|
|
36
|
-
send("#{action}!", finder_class,
|
37
|
+
send("#{action}!", finder_class, entity, params, options["finder.action"])
|
37
38
|
end
|
38
39
|
|
39
40
|
private
|
40
41
|
|
41
|
-
def all!(finder_class,
|
42
|
-
finder_class.new(
|
42
|
+
def all!(finder_class, entity, params, *)
|
43
|
+
finder_class.new(entity: entity, params: params)
|
43
44
|
end
|
44
45
|
|
45
|
-
def single!(finder_class,
|
46
|
+
def single!(finder_class, entity, params, *)
|
46
47
|
apply_id(params)
|
47
|
-
if
|
48
|
-
finder_class.new(
|
48
|
+
if entity.nil?
|
49
|
+
finder_class.new(params: params).result.first
|
49
50
|
else
|
50
|
-
finder_class.new(
|
51
|
+
finder_class.new(entity: entity, params: params).result.first
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
55
|
def apply_id(params)
|
55
56
|
return if params[:id].nil?
|
56
|
-
|
57
|
-
params[:
|
57
|
+
|
58
|
+
params[:id_eq] = params[:id] unless params.key?("id")
|
58
59
|
end
|
59
60
|
end
|
60
61
|
end
|