snfoil-rails 1.0.0.pre.1 → 1.0.2
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +201 -0
- data/README.md +207 -0
- data/lib/generators/snfoil/all/USAGE +21 -0
- data/lib/generators/snfoil/all/all_generator.rb +64 -0
- data/lib/generators/snfoil/context/USAGE +14 -0
- data/lib/generators/snfoil/context/context_generator.rb +55 -0
- data/lib/generators/snfoil/context/templates/context.erb +9 -0
- data/lib/generators/snfoil/controller/USAGE +15 -0
- data/lib/generators/snfoil/controller/controller_generator.rb +66 -0
- data/lib/generators/snfoil/controller/templates/api_controller.erb +14 -0
- data/lib/generators/snfoil/controller/templates/controller.erb +14 -0
- data/lib/generators/snfoil/jsonapi_deserializer/USAGE +14 -0
- data/lib/generators/snfoil/jsonapi_deserializer/jsonapi_deserializer_generator.rb +55 -0
- data/lib/generators/snfoil/jsonapi_deserializer/templates/jsonapi_deserializer.erb +12 -0
- data/lib/generators/snfoil/jsonapi_serializer/USAGE +14 -0
- data/lib/generators/snfoil/jsonapi_serializer/jsonapi_serializer_generator.rb +55 -0
- data/lib/generators/snfoil/jsonapi_serializer/templates/jsonapi_serializer.erb +20 -0
- data/lib/generators/snfoil/policy/USAGE +14 -0
- data/lib/generators/snfoil/policy/policy_generator.rb +55 -0
- data/lib/generators/snfoil/policy/templates/policy.erb +22 -0
- data/lib/generators/snfoil/searcher/USAGE +14 -0
- data/lib/generators/snfoil/searcher/searcher_generator.rb +55 -0
- data/lib/generators/snfoil/searcher/templates/searcher.erb +7 -0
- data/lib/snfoil/rails/api/create.rb +56 -0
- data/lib/snfoil/rails/api/destroy.rb +54 -0
- data/lib/snfoil/rails/api/index.rb +53 -0
- data/lib/snfoil/rails/api/show.rb +53 -0
- data/lib/snfoil/rails/api/update.rb +60 -0
- data/lib/snfoil/rails/api_controller.rb +33 -0
- data/lib/snfoil/rails/concerns/inject_deserialized.rb +31 -0
- data/lib/snfoil/rails/concerns/inject_id.rb +33 -0
- data/lib/snfoil/rails/concerns/inject_include.rb +35 -0
- data/lib/snfoil/rails/concerns/inject_request_id.rb +31 -0
- data/lib/snfoil/rails/concerns/inject_request_params.rb +34 -0
- data/lib/snfoil/rails/concerns/process_pagination.rb +71 -0
- data/lib/snfoil/rails/engine.rb +38 -0
- data/lib/snfoil/rails/searcher.rb +127 -0
- data/lib/snfoil/rails/version.rb +21 -0
- data/lib/snfoil/rails.rb +7 -0
- data/lib/tasks/snfoil/rails_tasks.rake +5 -0
- data/snfoil-rails.gemspec +53 -0
- metadata +86 -30
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative '../concerns/inject_id'
|
18
|
+
require_relative '../concerns/inject_include'
|
19
|
+
require_relative '../concerns/inject_request_params'
|
20
|
+
require_relative '../concerns/inject_request_id'
|
21
|
+
require 'snfoil/controller'
|
22
|
+
|
23
|
+
module SnFoil
|
24
|
+
module Rails
|
25
|
+
module API
|
26
|
+
module Show
|
27
|
+
extend ActiveSupport::Concern
|
28
|
+
|
29
|
+
included do
|
30
|
+
include SnFoil::Controller
|
31
|
+
|
32
|
+
include InjectRequestId
|
33
|
+
include InjectId
|
34
|
+
include InjectInclude
|
35
|
+
include InjectRequestParams
|
36
|
+
|
37
|
+
endpoint :show, with: :render_show
|
38
|
+
|
39
|
+
setup_show with: :inject_request_id
|
40
|
+
setup_show with: :inject_request_params
|
41
|
+
setup_show with: :inject_id
|
42
|
+
setup_show with: :inject_include
|
43
|
+
|
44
|
+
process_show with: :run_context
|
45
|
+
|
46
|
+
def render_show(**options)
|
47
|
+
render json: serialize(options[:object], **options), status: :ok
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative '../concerns/inject_deserialized'
|
18
|
+
require_relative '../concerns/inject_id'
|
19
|
+
require_relative '../concerns/inject_include'
|
20
|
+
require_relative '../concerns/inject_request_params'
|
21
|
+
require_relative '../concerns/inject_request_id'
|
22
|
+
require 'snfoil/controller'
|
23
|
+
|
24
|
+
module SnFoil
|
25
|
+
module Rails
|
26
|
+
module API
|
27
|
+
module Update
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
|
30
|
+
included do
|
31
|
+
include SnFoil::Controller
|
32
|
+
|
33
|
+
include InjectRequestId
|
34
|
+
include InjectDeserialized
|
35
|
+
include InjectId
|
36
|
+
include InjectInclude
|
37
|
+
include InjectRequestParams
|
38
|
+
|
39
|
+
endpoint :update, with: :render_update
|
40
|
+
|
41
|
+
setup_update with: :inject_request_id
|
42
|
+
setup_update with: :inject_request_params
|
43
|
+
setup_update with: :inject_id
|
44
|
+
setup_update with: :inject_deserialized
|
45
|
+
setup_update with: :inject_include
|
46
|
+
|
47
|
+
process_update with: :run_context
|
48
|
+
|
49
|
+
def render_update(**options)
|
50
|
+
if options[:object].errors.empty?
|
51
|
+
render json: serialize(options[:object], **options), status: :ok
|
52
|
+
else
|
53
|
+
render json: options[:object].errors, status: :unprocessable_entity
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative 'api/create'
|
18
|
+
require_relative 'api/destroy'
|
19
|
+
require_relative 'api/index'
|
20
|
+
require_relative 'api/show'
|
21
|
+
require_relative 'api/update'
|
22
|
+
|
23
|
+
module SnFoil
|
24
|
+
module Rails
|
25
|
+
class APIController < ActionController::API
|
26
|
+
include SnFoil::Rails::API::Create
|
27
|
+
include SnFoil::Rails::API::Show
|
28
|
+
include SnFoil::Rails::API::Update
|
29
|
+
include SnFoil::Rails::API::Destroy
|
30
|
+
include SnFoil::Rails::API::Index
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module InjectDeserialized
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def inject_deserialized(**options)
|
25
|
+
options[:params] = deserialize(options[:params], **options)
|
26
|
+
|
27
|
+
options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module InjectId
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def inject_id(**options)
|
25
|
+
return options if options[:id]
|
26
|
+
|
27
|
+
options[:id] = id if defined? id
|
28
|
+
options[:id] ||= options[:params][:id]
|
29
|
+
options
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module InjectInclude
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def inject_include(**options)
|
25
|
+
return options if options[:include]
|
26
|
+
return options unless options.dig(:params, :include)
|
27
|
+
|
28
|
+
options[:include] = options.dig(:params, :include)
|
29
|
+
.split(',')
|
30
|
+
.map { |item| item.underscore.to_sym }
|
31
|
+
options
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module InjectRequestId
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def inject_request_id(**options)
|
25
|
+
options[:request_id] = request.request_id
|
26
|
+
|
27
|
+
options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module InjectRequestParams
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def inject_request_params(**options)
|
25
|
+
return options if options[:params]
|
26
|
+
return options unless params
|
27
|
+
|
28
|
+
options[:params] = params.to_unsafe_h.deep_symbolize_keys
|
29
|
+
options[:request_params] = options[:params].deep_dup
|
30
|
+
options
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
|
19
|
+
module SnFoil
|
20
|
+
module Rails
|
21
|
+
module ProcessPagination
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
def paginate(**options)
|
25
|
+
return options[:object] unless options[:object].respond_to?(:page)
|
26
|
+
|
27
|
+
options[:object].page(pagination_page(**options))
|
28
|
+
.per(pagination_per_page(**options))
|
29
|
+
end
|
30
|
+
|
31
|
+
def pagination_page(**options)
|
32
|
+
(options.dig(:request_params, :page) || 1).to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def pagination_per_page(**options)
|
36
|
+
per_page_param = (options.dig(:request_params, :per_page) || 10).to_i
|
37
|
+
return 1000 if per_page_param.zero? || per_page_param > 1000
|
38
|
+
|
39
|
+
per_page_param
|
40
|
+
end
|
41
|
+
|
42
|
+
def pagination_count(**options)
|
43
|
+
options[:object].count if options[:object].respond_to? :count
|
44
|
+
end
|
45
|
+
|
46
|
+
def pagination_pages(**options)
|
47
|
+
count = pagination_count(**options)
|
48
|
+
per_page = pagination_per_page(**options)
|
49
|
+
return unless count && per_page
|
50
|
+
|
51
|
+
(count.to_f / per_page).ceil.to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
def pagination_meta(**options)
|
55
|
+
{
|
56
|
+
page: pagination_page(**options),
|
57
|
+
pages: pagination_pages(**options),
|
58
|
+
total: pagination_count(**options),
|
59
|
+
per: pagination_per_page(**options)
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def process_pagination(**options)
|
64
|
+
options[:meta] = pagination_meta(**options)
|
65
|
+
options[:object] = paginate(**options)
|
66
|
+
|
67
|
+
options
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
module Rails
|
19
|
+
class Engine < ::Rails::Engine
|
20
|
+
require 'snfoil'
|
21
|
+
require 'snfoil/policy'
|
22
|
+
require 'snfoil/searcher'
|
23
|
+
require 'snfoil/context'
|
24
|
+
require 'snfoil/controller'
|
25
|
+
require 'snfoil/deserializer/json'
|
26
|
+
require 'snfoil/deserializer/jsonapi'
|
27
|
+
|
28
|
+
require_relative 'api/create'
|
29
|
+
require_relative 'api/destroy'
|
30
|
+
require_relative 'api/index'
|
31
|
+
require_relative 'api/show'
|
32
|
+
require_relative 'api/update'
|
33
|
+
|
34
|
+
require_relative 'searcher'
|
35
|
+
require_relative 'api_controller'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'active_support/concern'
|
18
|
+
require 'snfoil/searcher'
|
19
|
+
|
20
|
+
module SnFoil
|
21
|
+
module Rails
|
22
|
+
module Searcher
|
23
|
+
extend ActiveSupport::Concern
|
24
|
+
ASC = 'ASC'
|
25
|
+
DESC = 'DESC'
|
26
|
+
|
27
|
+
included do
|
28
|
+
include SnFoil::Searcher
|
29
|
+
|
30
|
+
# patch the additional search capabilities into the method
|
31
|
+
def search(params = {})
|
32
|
+
filtered_scope = super(**params)
|
33
|
+
additional_search(filtered_scope, params)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class_methods do
|
38
|
+
attr_reader :snfoil_include_params, :snfoil_order_method, :snfoil_order_block,
|
39
|
+
:snfoil_order_by_attr, :snfoil_order_by_direction, :snfoil_is_distinct
|
40
|
+
|
41
|
+
def order(method = nil, &block)
|
42
|
+
@snfoil_order_method = method
|
43
|
+
@snfoil_order_block = block
|
44
|
+
end
|
45
|
+
|
46
|
+
def order_by(attr, direction = nil)
|
47
|
+
@snfoil_order_by_attr = attr
|
48
|
+
@snfoil_order_by_direction = direction
|
49
|
+
end
|
50
|
+
|
51
|
+
def distinct(bool = true) # rubocop:disable Style/OptionalBooleanParameter reason: class configuration looks better this way
|
52
|
+
@snfoil_is_distinct = bool
|
53
|
+
end
|
54
|
+
|
55
|
+
def includes(*array)
|
56
|
+
@snfoil_include_params ||= [] # create new array if none exists
|
57
|
+
@snfoil_include_params |= array # combine unique elements of both arrays
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def order_by(params = {})
|
62
|
+
if params[:order_by].present?
|
63
|
+
params[:order_by] = params[:order_by].to_s.underscore
|
64
|
+
return params[:order_by].to_sym if model.attribute_names.include?(params[:order_by])
|
65
|
+
end
|
66
|
+
|
67
|
+
self.class.snfoil_order_by_attr || :id
|
68
|
+
end
|
69
|
+
|
70
|
+
def order(params = {})
|
71
|
+
if params[:order].present?
|
72
|
+
params[:order] = params[:order].to_s.upcase
|
73
|
+
return params[:order] if params[:order].eql?(ASC) || params[:order].eql?(DESC)
|
74
|
+
end
|
75
|
+
|
76
|
+
self.class.snfoil_order_by_direction || ASC
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def additional_search(filtered_scope, params = {})
|
82
|
+
filtered_scope = apply_order(filtered_scope, params)
|
83
|
+
filtered_scope = apply_includes(filtered_scope)
|
84
|
+
apply_distinct(filtered_scope, params)
|
85
|
+
end
|
86
|
+
|
87
|
+
def apply_includes(filtered_scope)
|
88
|
+
return filtered_scope unless self.class.snfoil_include_params
|
89
|
+
|
90
|
+
filtered_scope.includes(*self.class.snfoil_include_params)
|
91
|
+
end
|
92
|
+
|
93
|
+
def apply_order(filtered_scope, params)
|
94
|
+
return order_method(filtered_scope, params) if order_method_present?
|
95
|
+
return order_block(filtered_scope, params) if order_block_present?
|
96
|
+
|
97
|
+
if params[:order_by].blank? && params[:order].blank?
|
98
|
+
filtered_scope.order(order_by => order)
|
99
|
+
else
|
100
|
+
filtered_scope.order(order_by(params) => order(params))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def order_method_present?
|
105
|
+
self.class.snfoil_order_method.present?
|
106
|
+
end
|
107
|
+
|
108
|
+
def order_block_present?
|
109
|
+
self.class.snfoil_order_block.present?
|
110
|
+
end
|
111
|
+
|
112
|
+
def order_method(filtered_scope, params)
|
113
|
+
send(self.class.snfoil_order_method, filtered_scope, params)
|
114
|
+
end
|
115
|
+
|
116
|
+
def order_block(filtered_scope, params)
|
117
|
+
instance_exec filtered_scope, params, &self.class.snfoil_order_block
|
118
|
+
end
|
119
|
+
|
120
|
+
def apply_distinct(filtered_scope, params)
|
121
|
+
return filtered_scope unless self.class.snfoil_is_distinct || params[:distinct] == true
|
122
|
+
|
123
|
+
filtered_scope.distinct
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
module Rails
|
19
|
+
VERSION = '1.0.2'
|
20
|
+
end
|
21
|
+
end
|
data/lib/snfoil/rails.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/snfoil/rails/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'snfoil-rails'
|
7
|
+
spec.version = SnFoil::Rails::VERSION
|
8
|
+
spec.authors = ['Matthew Howes', 'Danny Murphy', 'Cliff Campbell']
|
9
|
+
spec.email = ['howeszy@gmail.com', 'dmurph24@gmail.com', 'cliffcampbell@hey.com']
|
10
|
+
|
11
|
+
spec.summary = 'Snfoil Rails Helpers'
|
12
|
+
spec.description = 'Additional functionality gem for using SnFoil with Rails'
|
13
|
+
spec.homepage = 'https://github.com/limited-effort/snfoil-rails'
|
14
|
+
spec.license = 'Apache-2.0'
|
15
|
+
spec.required_ruby_version = '>= 2.6'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/limited-effort/snfoil-rails/blob/main/CHANGELOG.md'
|
20
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
ignore_list = %r{\A(?:test/|spec/|bin/|features/|Rakefile|\.\w)}
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) { `git ls-files -z`.split("\x0").reject { |f| f.match(ignore_list) } }
|
26
|
+
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_dependency 'activesupport', '>= 5.2.6'
|
32
|
+
spec.add_dependency 'snfoil', '>= 1.0.1', '< 2.0'
|
33
|
+
spec.add_dependency 'snfoil-controller', '>= 1.1.1', '< 2.0'
|
34
|
+
|
35
|
+
spec.add_development_dependency 'blueprinter'
|
36
|
+
spec.add_development_dependency 'bundle-audit', '~> 0.1.0'
|
37
|
+
spec.add_development_dependency 'database_cleaner-active_record'
|
38
|
+
spec.add_development_dependency 'factory_bot', '~> 6.0'
|
39
|
+
spec.add_development_dependency 'fasterer', '~> 0.10.0'
|
40
|
+
spec.add_development_dependency 'kaminari'
|
41
|
+
spec.add_development_dependency 'net-smtp'
|
42
|
+
spec.add_development_dependency 'oj'
|
43
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.9'
|
44
|
+
spec.add_development_dependency 'puma'
|
45
|
+
spec.add_development_dependency 'rails', '~> 6.0'
|
46
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
47
|
+
spec.add_development_dependency 'rspec-rails', '~> 5.0'
|
48
|
+
spec.add_development_dependency 'rubocop', '1.33'
|
49
|
+
spec.add_development_dependency 'rubocop-performance', '1.14.3'
|
50
|
+
spec.add_development_dependency 'rubocop-rails', '~> 2.14'
|
51
|
+
spec.add_development_dependency 'rubocop-rspec', '2.12.1'
|
52
|
+
spec.add_development_dependency 'sqlite3'
|
53
|
+
end
|