golden-objects 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +15 -3
- data/golden-objects.gemspec +1 -1
- data/lib/golden/action_view/extension.rb +2 -0
- data/lib/golden/action_view/form_builder.rb +10 -0
- data/lib/golden/action_view/form_helper.rb +12 -0
- data/lib/golden/objects/application/application_presenter.rb +1 -0
- data/lib/golden/objects/query/query_context.rb +35 -5
- data/lib/golden/objects/query/query_form.rb +3 -0
- data/lib/golden/objects/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4de69f92ff7c1ce061a32efdd6a205ee59f19e099d11572d7f257e838d578ac
|
4
|
+
data.tar.gz: ea05875072e65491a83eee3c1c17c94ef83cfb0a8f722b8a815cccd4004250a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2729a0e5e57c3efa63e83d02772e386716dfc750abf68bba40c06d37ec546cdc1c2dbd4560657bac81afcc4c67dd0805f3fd7aa1a6ab5ed636548b2ee995edfb
|
7
|
+
data.tar.gz: 8564a13caf8cc5b3f04ee5213ffd10acd72f5d3cec1a2741bc3794a52cef9b7dc2e1ce7a74842fe978832c1b35f722f81f83aa1cc80169238ae1d3e35c9f1ac6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
Create your class and inherite appropriate golden object directly or
|
25
|
+
Create your class and inherite appropriate golden object directly or though another class.
|
26
26
|
|
27
27
|
```
|
28
28
|
class ApplicationPresenter < Golden::ApplicationPresenter
|
@@ -61,15 +61,16 @@ app/objects/orders/create_operator.rb
|
|
61
61
|
|
62
62
|
## Modules
|
63
63
|
|
64
|
-
|
64
|
+
4 kinds of modules are provided.
|
65
65
|
|
66
66
|
* attribute accessors
|
67
67
|
* active model concerns
|
68
68
|
* active record concerns
|
69
|
+
* action view extensions
|
69
70
|
|
70
71
|
## Objects
|
71
72
|
|
72
|
-
3 groups of objects are
|
73
|
+
3 groups of objects are provided.
|
73
74
|
|
74
75
|
* Application objects
|
75
76
|
* Golden::ApplicationCalculator
|
@@ -90,6 +91,17 @@ app/objects/orders/create_operator.rb
|
|
90
91
|
* Golden::QueryFormOperator
|
91
92
|
* Golden::QueryResultPresenter
|
92
93
|
|
94
|
+
## Action View
|
95
|
+
|
96
|
+
require extension for actionview in controller of rails.
|
97
|
+
|
98
|
+
```
|
99
|
+
require 'golden/action_view/extension'
|
100
|
+
|
101
|
+
class ApplicationController < ActionController::Base
|
102
|
+
helper ::Golden::FormHelper
|
103
|
+
```
|
104
|
+
|
93
105
|
## Development
|
94
106
|
|
95
107
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/golden-objects.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
# spec.metadata['allowed_push_host'] = 'https://github.com'
|
16
16
|
spec.metadata['homepage_uri'] = spec.homepage
|
17
17
|
spec.metadata['source_code_uri'] = 'https://github.com/goldenio/golden-objects.git'
|
18
|
-
spec.metadata['changelog_uri'] = 'https://github.com/goldenio/golden-objects/CHANGELOG.md'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/goldenio/golden-objects/blob/master/CHANGELOG.md'
|
19
19
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Golden
|
4
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
5
|
+
def label(method, text = nil, options = {}, &block)
|
6
|
+
text ||= I18n.t("helpers.label.#{object.model_name.i18n_key}.#{method}")
|
7
|
+
super(method, text, options, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Golden
|
4
|
+
module FormHelper
|
5
|
+
def golden_form_with(**options, &block)
|
6
|
+
model = options[:model]
|
7
|
+
default_options = { builder: ::Golden::FormBuilder }
|
8
|
+
default_options.merge!({ scope: :query, url: model.query_url }) if model.respond_to? :query_url
|
9
|
+
form_with(**default_options.merge(options), &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -12,13 +12,18 @@ module Golden
|
|
12
12
|
attr_accessor :page, :per
|
13
13
|
attr_accessor :sort_field, :sort_direction
|
14
14
|
|
15
|
+
boolean_accessor :plucking
|
16
|
+
attr_reader :pluck_fields
|
17
|
+
|
15
18
|
def initialize(accessors = {})
|
16
19
|
assign_attributes(accessors || {})
|
17
20
|
@page ||= 1
|
18
21
|
@per ||= 100
|
19
22
|
end
|
20
23
|
|
21
|
-
def perform(mode = :paginated)
|
24
|
+
def perform(mode = :paginated, pluck: nil)
|
25
|
+
@plucking = pluck.present?
|
26
|
+
@pluck_fields = pluck
|
22
27
|
send("find_#{mode}")
|
23
28
|
end
|
24
29
|
|
@@ -47,7 +52,8 @@ module Golden
|
|
47
52
|
def find_all
|
48
53
|
return [] if invalid?
|
49
54
|
|
50
|
-
relations.where(query_scopes).order(sort).all
|
55
|
+
finder = relations.where(query_scopes).order(sort).all
|
56
|
+
find_or_pluck(finder)
|
51
57
|
end
|
52
58
|
|
53
59
|
def find_count
|
@@ -59,7 +65,8 @@ module Golden
|
|
59
65
|
def find_paginated
|
60
66
|
return paginated_blank_result if invalid?
|
61
67
|
|
62
|
-
relations.where(query_scopes).order(sort).page(page).per(per)
|
68
|
+
finder = relations.where(query_scopes).order(sort).page(page).per(per)
|
69
|
+
find_or_pluck(finder)
|
63
70
|
end
|
64
71
|
|
65
72
|
def paginated_blank_result
|
@@ -68,6 +75,22 @@ module Golden
|
|
68
75
|
raise NotImplementedError
|
69
76
|
end
|
70
77
|
|
78
|
+
def find_or_pluck(finder)
|
79
|
+
return finder unless plucking?
|
80
|
+
|
81
|
+
finder.pluck(safe_table_fields(pluck_fields) || Arel.star)
|
82
|
+
end
|
83
|
+
|
84
|
+
def safe_table_fields(table_and_fields)
|
85
|
+
table_fields = table_and_fields.flat_map do |table, fields|
|
86
|
+
table_name = send(table)&.name
|
87
|
+
next if table_name.blank?
|
88
|
+
|
89
|
+
fields.map { |field| [table_name, field.to_s].join('.') }
|
90
|
+
end.compact
|
91
|
+
Arel.sql(table_fields.join(', '))
|
92
|
+
end
|
93
|
+
|
71
94
|
def relations
|
72
95
|
raise NotImplementedError, <<~ERROR
|
73
96
|
Please define #{__method__} like
|
@@ -86,7 +109,9 @@ module Golden
|
|
86
109
|
def #{__method__}
|
87
110
|
@sort_field ||= :id
|
88
111
|
@sort_direction ||= :desc
|
89
|
-
@#{__method__} ||=
|
112
|
+
@#{__method__} ||= [
|
113
|
+
Record.arel_table[@sort_field].send(@sort_direction)
|
114
|
+
]
|
90
115
|
end
|
91
116
|
```
|
92
117
|
ERROR
|
@@ -97,6 +122,7 @@ module Golden
|
|
97
122
|
Please define #{__method__} like
|
98
123
|
```
|
99
124
|
def #{__method__}
|
125
|
+
@scopes = nil
|
100
126
|
concat_xxx_scope
|
101
127
|
@scopes
|
102
128
|
end
|
@@ -105,10 +131,14 @@ module Golden
|
|
105
131
|
```
|
106
132
|
def concat_xxx_scope
|
107
133
|
scope = Record.arel_table[:xxx_number].eq(@xxx_number)
|
108
|
-
|
134
|
+
scopes_and(scope)
|
109
135
|
end
|
110
136
|
```
|
111
137
|
ERROR
|
112
138
|
end
|
139
|
+
|
140
|
+
def scopes_and(scope)
|
141
|
+
@scopes = @scopes ? @scopes.and(scope) : scope
|
142
|
+
end
|
113
143
|
end
|
114
144
|
end
|
@@ -17,6 +17,7 @@ module Golden
|
|
17
17
|
|
18
18
|
attr_reader :result
|
19
19
|
attr_accessor :per, :page, :sort
|
20
|
+
attr_accessor :mode
|
20
21
|
|
21
22
|
def initialize(params, accessors = {})
|
22
23
|
@query_params = find_query_from(params)
|
@@ -25,6 +26,7 @@ module Golden
|
|
25
26
|
@per ||= params.dig(:per)
|
26
27
|
@page ||= params.dig(:page)
|
27
28
|
@sort ||= params.dig(:sort)
|
29
|
+
@mode ||= :paginated
|
28
30
|
end
|
29
31
|
|
30
32
|
def save
|
@@ -48,6 +50,7 @@ module Golden
|
|
48
50
|
|
49
51
|
def query_accessors
|
50
52
|
attrs = attributes.clone
|
53
|
+
attrs.delete(:mode)
|
51
54
|
attrs.each { |accessor, value| attrs.delete(accessor) if value.blank? }
|
52
55
|
attrs
|
53
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: golden-objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tse-Ching Ho
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -69,6 +69,9 @@ files:
|
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
71
|
- golden-objects.gemspec
|
72
|
+
- lib/golden/action_view/extension.rb
|
73
|
+
- lib/golden/action_view/form_builder.rb
|
74
|
+
- lib/golden/action_view/form_helper.rb
|
72
75
|
- lib/golden/active_model_concerns.rb
|
73
76
|
- lib/golden/active_model_concerns/datepicker_concern.rb
|
74
77
|
- lib/golden/active_model_concerns/has_persisted_record_concern.rb
|
@@ -106,7 +109,7 @@ licenses:
|
|
106
109
|
metadata:
|
107
110
|
homepage_uri: https://github.com/goldenio/golden-objects
|
108
111
|
source_code_uri: https://github.com/goldenio/golden-objects.git
|
109
|
-
changelog_uri: https://github.com/goldenio/golden-objects/CHANGELOG.md
|
112
|
+
changelog_uri: https://github.com/goldenio/golden-objects/blob/master/CHANGELOG.md
|
110
113
|
post_install_message:
|
111
114
|
rdoc_options: []
|
112
115
|
require_paths:
|
@@ -122,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
125
|
- !ruby/object:Gem::Version
|
123
126
|
version: '0'
|
124
127
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
128
|
+
rubygems_version: 3.0.6
|
126
129
|
signing_key:
|
127
130
|
specification_version: 4
|
128
131
|
summary: Provide ruby classes and modules help you build business logics as ruby components.
|