golden-objects 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4854525e660454fcdcd717c48e05addb718eb6571e7ee2203068587968bc570d
4
- data.tar.gz: b12b5850a5da5b637fe56005acf318a22085347dd9a22483dfc52bd24204978e
3
+ metadata.gz: d4de69f92ff7c1ce061a32efdd6a205ee59f19e099d11572d7f257e838d578ac
4
+ data.tar.gz: ea05875072e65491a83eee3c1c17c94ef83cfb0a8f722b8a815cccd4004250a3
5
5
  SHA512:
6
- metadata.gz: ab570549fa989c52516de15a1c87a2439f3aba9928ef5563f9c9e02e82daa270c88f8c55cbd9936eaff22c23a6147a731d3bb3299f56b96fc17cbf50fb50324d
7
- data.tar.gz: d149d32f6989842cb7407323d56a160651cb820f030f5b9ce5b649bfefa6ca7202ed5892e18e23745b3a9bb7c9fc9c6d6826e0088904ce0bf877bdc57bee693c
6
+ metadata.gz: 2729a0e5e57c3efa63e83d02772e386716dfc750abf68bba40c06d37ec546cdc1c2dbd4560657bac81afcc4c67dd0805f3fd7aa1a6ab5ed636548b2ee995edfb
7
+ data.tar.gz: 8564a13caf8cc5b3f04ee5213ffd10acd72f5d3cec1a2741bc3794a52cef9b7dc2e1ce7a74842fe978832c1b35f722f81f83aa1cc80169238ae1d3e35c9f1ac6
@@ -1,5 +1,10 @@
1
1
  # Change Logs
2
2
 
3
+ ### v0.2.0
4
+
5
+ * Add golden form builder and helper
6
+ * Golden query context support pluck
7
+
3
8
  ### v0.1.0
4
9
 
5
10
  * Initial release.
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 thought another class.
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
- 3 kinds of modules are provied.
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 provied.
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.
@@ -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,2 @@
1
+ require 'golden/action_view/form_builder'
2
+ require 'golden/action_view/form_helper'
@@ -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
@@ -4,6 +4,7 @@ require 'golden/attribute_accessors'
4
4
 
5
5
  module Golden
6
6
  class ApplicationPresenter
7
+ extend ActiveModel::Naming
7
8
  extend ActiveModel::Translation
8
9
  include ActiveModel::AttributeAssignment
9
10
 
@@ -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__} ||= Record.arel_table[@sort_field].send(@sort_direction)
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
- @scopes = @scopes ? @scopes.and(scope) : scope
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
@@ -1,5 +1,5 @@
1
1
  module Golden
2
2
  module Objects
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.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.1.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-03-26 00:00:00.000000000 Z
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.1.2
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.