golden-objects 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: d4de69f92ff7c1ce061a32efdd6a205ee59f19e099d11572d7f257e838d578ac
4
- data.tar.gz: ea05875072e65491a83eee3c1c17c94ef83cfb0a8f722b8a815cccd4004250a3
3
+ metadata.gz: fb87d44d24edcf4229613926623a663f47715af02ec287a84d87618480e0bf61
4
+ data.tar.gz: fb652ede3b1a0535be872ebc595c663ceb12b8f205e24a23d114d4c93b2352ea
5
5
  SHA512:
6
- metadata.gz: 2729a0e5e57c3efa63e83d02772e386716dfc750abf68bba40c06d37ec546cdc1c2dbd4560657bac81afcc4c67dd0805f3fd7aa1a6ab5ed636548b2ee995edfb
7
- data.tar.gz: 8564a13caf8cc5b3f04ee5213ffd10acd72f5d3cec1a2741bc3794a52cef9b7dc2e1ce7a74842fe978832c1b35f722f81f83aa1cc80169238ae1d3e35c9f1ac6
6
+ metadata.gz: 3ec0d2f10fb89604eecc12e34dc5a26ccde337af47eda00ee2b74f9c7dbb43b5ceb1a62209a5d99a34e0dff5516343ca38d268840af240147f3ccf2cd765e9c8
7
+ data.tar.gz: 00b23f3b130d76eb08bb63b3c1037838e4de74e9b882d7e6003bed652fa83439663c5efb073dccf8aa05ebcd886dea6201b0db361e8d42efe5e56e2e1739fd2e
@@ -1,5 +1,20 @@
1
1
  # Change Logs
2
2
 
3
+ ### v0.3.0
4
+
5
+ * `Golden::QueryResultPresenter`:
6
+ * BREAKING: Change `initialize` definition.
7
+ * Add `records`, `presenter_class` accessors.
8
+ * Add `collect`, `paginated_array` class methods.
9
+ * Let `presenters` can be customized.
10
+ * Add missing `total_page` method.
11
+ * `Golden::QueryFormOperator`:
12
+ * Add `mode` accessor and pass to form.
13
+ * Let `query_result`, `record_presenter_class` can be customized.
14
+ * `Golden::QueryContext`:
15
+ * Include `ActiveRecord::Sanitization::ClassMethods`.
16
+ * Change prefered `sort` implementation.
17
+
3
18
  ### v0.2.0
4
19
 
5
20
  * Add golden form builder and helper
@@ -9,6 +9,8 @@ module Golden
9
9
  end
10
10
  end
11
11
 
12
+ include ::ActiveRecord::Sanitization::ClassMethods
13
+
12
14
  attr_accessor :page, :per
13
15
  attr_accessor :sort_field, :sort_direction
14
16
 
@@ -109,8 +111,16 @@ module Golden
109
111
  def #{__method__}
110
112
  @sort_field ||= :id
111
113
  @sort_direction ||= :desc
112
- @#{__method__} ||= [
113
- Record.arel_table[@sort_field].send(@sort_direction)
114
+ @#{__method__} = send("sort_by_\#{@sort_field}")
115
+ rescue NoMethodError
116
+ @#{__method__} = sort_by_id
117
+ end
118
+ ```
119
+ And define sort_by_xxx like
120
+ ```
121
+ def sort_by_id
122
+ [
123
+ Record.arel_table[sort_field].send(sort_direction)
114
124
  ]
115
125
  end
116
126
  ```
@@ -56,7 +56,16 @@ module Golden
56
56
  end
57
57
 
58
58
  def query!
59
- raise NotImplementedError
59
+ raise NotImplementedError, <<~ERROR
60
+ Please define #{__method__} like
61
+ ```
62
+ def #{__method__}
63
+ context = ::Orders::QueryContext.new query_accessors
64
+ @result = context.perform(mode)
65
+ errors.merge! context.errors
66
+ end
67
+ ```
68
+ ERROR
60
69
  end
61
70
  end
62
71
  end
@@ -4,7 +4,7 @@ module Golden
4
4
  class QueryFormOperator < Golden::ApplicationOperator
5
5
  class << self
6
6
  # rubocop:disable Naming/PredicateName
7
- def has_query_form(form: nil, form_presenter: nil, result_presenter: nil)
7
+ def has_query_form(form: nil, form_presenter: nil, result_presenter: nil, record_presenter: nil)
8
8
  define_method :form_class do
9
9
  return form.to_s.constantize if form.present?
10
10
 
@@ -22,6 +22,12 @@ module Golden
22
22
 
23
23
  "#{self.class.parent.name}::QueryResultPresenter".constantize
24
24
  end
25
+
26
+ define_method :record_presenter_class do
27
+ return record_presenter.to_s.constantize if record_presenter.present?
28
+
29
+ "#{self.class.parent.name}::RecordPresenter".constantize
30
+ end
25
31
  end
26
32
  # rubocop:enable Naming/PredicateName
27
33
  end
@@ -30,6 +36,7 @@ module Golden
30
36
 
31
37
  define_model_callbacks :build_form
32
38
 
39
+ attr_accessor :mode
33
40
  attr_reader :params, :form
34
41
 
35
42
  def initialize(params, accessors = {})
@@ -49,7 +56,7 @@ module Golden
49
56
  end
50
57
 
51
58
  def result_presenter
52
- @result_presenter ||= result_presenter_class.collect form.result
59
+ @result_presenter ||= result_presenter_class.collect query_result, record_presenter_class
53
60
  end
54
61
 
55
62
  private
@@ -58,6 +65,16 @@ module Golden
58
65
  params
59
66
  end
60
67
 
68
+ def query_accessors
69
+ {
70
+ mode: mode
71
+ }
72
+ end
73
+
74
+ def query_result
75
+ form.result
76
+ end
77
+
61
78
  def build_form
62
79
  form_accessors = send(:query_accessors) if private_methods.include? :query_accessors
63
80
  form_class.new query_params, form_accessors
@@ -2,23 +2,43 @@
2
2
 
3
3
  module Golden
4
4
  class QueryResultPresenter < Golden::ApplicationPresenter
5
- include Enumerable
5
+ class << self
6
+ def collect(result, presenter_class)
7
+ new(
8
+ records: paginated_array(result),
9
+ presenter_class: presenter_class
10
+ )
11
+ end
6
12
 
7
- attr_reader :records, :presenters
13
+ def paginated_array(result)
14
+ return result unless result.is_a?(Array)
15
+ return result unless ::Object.const_defined? 'Kaminari'
16
+ return Kaminari.paginate_array([], total_count: 0).page(1) if result.empty?
8
17
 
9
- def initialize(records, presenter_class)
10
- @records = records
11
- @presenters = records.map { |record| presenter_class.constantize.new record }
18
+ Kaminari.paginate_array(result).page(1).per(result.size)
19
+ end
12
20
  end
13
21
 
22
+ include Enumerable
23
+
24
+ attr_accessor :records, :presenter_class
25
+
14
26
  def each(&block)
15
27
  presenters.each(&block)
16
28
  end
17
29
 
30
+ def presenters
31
+ @presenters ||= records.map { |record| presenter_class.constantize.new record }
32
+ end
33
+
18
34
  def total_count
19
35
  records.total_count
20
36
  end
21
37
 
38
+ def total_page
39
+ records.total_page
40
+ end
41
+
22
42
  def current_page
23
43
  records.current_page
24
44
  end
@@ -1,5 +1,5 @@
1
1
  module Golden
2
2
  module Objects
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  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.2.0
4
+ version: 0.3.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-05-27 00:00:00.000000000 Z
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  requirements: []
128
- rubygems_version: 3.0.6
128
+ rubygems_version: 3.1.2
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Provide ruby classes and modules help you build business logics as ruby components.