neewom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d0ffd96cee9a740737d32948b45474858e28444c138e8a748f457985c5029541
4
+ data.tar.gz: fa6e08472a06a00da8dfda822b815031ac25a4773d0181fb91b1b2e00b365d7a
5
+ SHA512:
6
+ metadata.gz: 59d8669ecadba1e83624b7edd69eeb907b9e2b2c75dad39ccfc91e02dac790dd94ee6fe80695f1e11ac32b04b0768520555e1530bc0ad113c5e3e18b83506b93
7
+ data.tar.gz: e030a33140221b88e775d1db3612cb233c35e03c204885ca6d47ac9c54f44d1e1d6f4217d70c02c0262ae58c26649a955cb911ac15c0e700404053330d79fdac
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at olegz@jetruby.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ group :development, :test do
6
+ gem 'rails-dummy'
7
+ gem 'pg'
8
+ gem 'rails'
9
+ gem 'byebug'
10
+ gem 'pry'
11
+ end
12
+
13
+ group :test do
14
+ gem 'rspec-rails'
15
+ gem 'database_cleaner'
16
+ gem 'simplecov'
17
+ end
18
+
19
+
20
+ # Specify your gem's dependencies in neewom.gemspec
21
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,189 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ neewom (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.0.0)
10
+ actionpack (= 6.0.0)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (>= 0.6.1)
13
+ actionmailbox (6.0.0)
14
+ actionpack (= 6.0.0)
15
+ activejob (= 6.0.0)
16
+ activerecord (= 6.0.0)
17
+ activestorage (= 6.0.0)
18
+ activesupport (= 6.0.0)
19
+ mail (>= 2.7.1)
20
+ actionmailer (6.0.0)
21
+ actionpack (= 6.0.0)
22
+ actionview (= 6.0.0)
23
+ activejob (= 6.0.0)
24
+ mail (~> 2.5, >= 2.5.4)
25
+ rails-dom-testing (~> 2.0)
26
+ actionpack (6.0.0)
27
+ actionview (= 6.0.0)
28
+ activesupport (= 6.0.0)
29
+ rack (~> 2.0)
30
+ rack-test (>= 0.6.3)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
33
+ actiontext (6.0.0)
34
+ actionpack (= 6.0.0)
35
+ activerecord (= 6.0.0)
36
+ activestorage (= 6.0.0)
37
+ activesupport (= 6.0.0)
38
+ nokogiri (>= 1.8.5)
39
+ actionview (6.0.0)
40
+ activesupport (= 6.0.0)
41
+ builder (~> 3.1)
42
+ erubi (~> 1.4)
43
+ rails-dom-testing (~> 2.0)
44
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
45
+ activejob (6.0.0)
46
+ activesupport (= 6.0.0)
47
+ globalid (>= 0.3.6)
48
+ activemodel (6.0.0)
49
+ activesupport (= 6.0.0)
50
+ activerecord (6.0.0)
51
+ activemodel (= 6.0.0)
52
+ activesupport (= 6.0.0)
53
+ activestorage (6.0.0)
54
+ actionpack (= 6.0.0)
55
+ activejob (= 6.0.0)
56
+ activerecord (= 6.0.0)
57
+ marcel (~> 0.3.1)
58
+ activesupport (6.0.0)
59
+ concurrent-ruby (~> 1.0, >= 1.0.2)
60
+ i18n (>= 0.7, < 2)
61
+ minitest (~> 5.1)
62
+ tzinfo (~> 1.1)
63
+ zeitwerk (~> 2.1, >= 2.1.8)
64
+ builder (3.2.3)
65
+ byebug (11.0.1)
66
+ coderay (1.1.2)
67
+ concurrent-ruby (1.1.5)
68
+ crass (1.0.4)
69
+ database_cleaner (1.7.0)
70
+ diff-lcs (1.3)
71
+ docile (1.3.2)
72
+ erubi (1.8.0)
73
+ globalid (0.4.2)
74
+ activesupport (>= 4.2.0)
75
+ i18n (1.6.0)
76
+ concurrent-ruby (~> 1.0)
77
+ json (2.2.0)
78
+ loofah (2.2.3)
79
+ crass (~> 1.0.2)
80
+ nokogiri (>= 1.5.9)
81
+ mail (2.7.1)
82
+ mini_mime (>= 0.1.1)
83
+ marcel (0.3.3)
84
+ mimemagic (~> 0.3.2)
85
+ method_source (0.9.2)
86
+ mimemagic (0.3.3)
87
+ mini_mime (1.0.2)
88
+ mini_portile2 (2.4.0)
89
+ minitest (5.11.3)
90
+ nio4r (2.5.1)
91
+ nokogiri (1.10.4)
92
+ mini_portile2 (~> 2.4.0)
93
+ pg (1.1.4)
94
+ pry (0.12.2)
95
+ coderay (~> 1.1.0)
96
+ method_source (~> 0.9.0)
97
+ rack (2.0.7)
98
+ rack-test (1.1.0)
99
+ rack (>= 1.0, < 3)
100
+ rails (6.0.0)
101
+ actioncable (= 6.0.0)
102
+ actionmailbox (= 6.0.0)
103
+ actionmailer (= 6.0.0)
104
+ actionpack (= 6.0.0)
105
+ actiontext (= 6.0.0)
106
+ actionview (= 6.0.0)
107
+ activejob (= 6.0.0)
108
+ activemodel (= 6.0.0)
109
+ activerecord (= 6.0.0)
110
+ activestorage (= 6.0.0)
111
+ activesupport (= 6.0.0)
112
+ bundler (>= 1.3.0)
113
+ railties (= 6.0.0)
114
+ sprockets-rails (>= 2.0.0)
115
+ rails-dom-testing (2.0.3)
116
+ activesupport (>= 4.2.0)
117
+ nokogiri (>= 1.6)
118
+ rails-dummy (0.1.0)
119
+ railties
120
+ rails-html-sanitizer (1.2.0)
121
+ loofah (~> 2.2, >= 2.2.2)
122
+ railties (6.0.0)
123
+ actionpack (= 6.0.0)
124
+ activesupport (= 6.0.0)
125
+ method_source
126
+ rake (>= 0.8.7)
127
+ thor (>= 0.20.3, < 2.0)
128
+ rake (10.5.0)
129
+ rspec (3.8.0)
130
+ rspec-core (~> 3.8.0)
131
+ rspec-expectations (~> 3.8.0)
132
+ rspec-mocks (~> 3.8.0)
133
+ rspec-core (3.8.2)
134
+ rspec-support (~> 3.8.0)
135
+ rspec-expectations (3.8.4)
136
+ diff-lcs (>= 1.2.0, < 2.0)
137
+ rspec-support (~> 3.8.0)
138
+ rspec-mocks (3.8.1)
139
+ diff-lcs (>= 1.2.0, < 2.0)
140
+ rspec-support (~> 3.8.0)
141
+ rspec-rails (3.8.2)
142
+ actionpack (>= 3.0)
143
+ activesupport (>= 3.0)
144
+ railties (>= 3.0)
145
+ rspec-core (~> 3.8.0)
146
+ rspec-expectations (~> 3.8.0)
147
+ rspec-mocks (~> 3.8.0)
148
+ rspec-support (~> 3.8.0)
149
+ rspec-support (3.8.2)
150
+ simplecov (0.17.1)
151
+ docile (~> 1.1)
152
+ json (>= 1.8, < 3)
153
+ simplecov-html (~> 0.10.0)
154
+ simplecov-html (0.10.2)
155
+ sprockets (3.7.2)
156
+ concurrent-ruby (~> 1.0)
157
+ rack (> 1, < 3)
158
+ sprockets-rails (3.2.1)
159
+ actionpack (>= 4.0)
160
+ activesupport (>= 4.0)
161
+ sprockets (>= 3.0.0)
162
+ thor (0.20.3)
163
+ thread_safe (0.3.6)
164
+ tzinfo (1.2.5)
165
+ thread_safe (~> 0.1)
166
+ websocket-driver (0.7.1)
167
+ websocket-extensions (>= 0.1.0)
168
+ websocket-extensions (0.1.4)
169
+ zeitwerk (2.1.10)
170
+
171
+ PLATFORMS
172
+ ruby
173
+
174
+ DEPENDENCIES
175
+ bundler (~> 1.17)
176
+ byebug
177
+ database_cleaner
178
+ neewom!
179
+ pg
180
+ pry
181
+ rails
182
+ rails-dummy
183
+ rake (~> 10.0)
184
+ rspec (~> 3.0)
185
+ rspec-rails
186
+ simplecov
187
+
188
+ BUNDLED WITH
189
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 KnightWhoSayNi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,318 @@
1
+ # Neewom
2
+
3
+ Rails custom form builder. Was designed to solve general issues about dynamic attributes:
4
+
5
+ - Ability to have different fields on the same form for different users
6
+ - Ability to allow your users to add a custom fields on forms they need
7
+ - Ability to search by custom data.
8
+
9
+ # Custom fields for different users.
10
+
11
+ Before building a custom attributes system (which is usually takes about 60 hours), developers goes by a simple way and just add new column to the database.
12
+ It's a common use case, when some customer is ready to pay for your service, but if you will add some specific fields on some forms. And often other users doesn't need those fields.
13
+
14
+ This approach still a good one and the most simplest and cheapest. But, with growing a number of users, who needs their own custom fields, it becomes a pain.
15
+
16
+ Neewom is a flexible solution which allows to organize your custom forms.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'neewom'
24
+ ```
25
+
26
+ Copy a default form template
27
+
28
+ **app/views/neewom_forms/form.html.erb**
29
+
30
+ ```ruby
31
+ <%= form_for @resource, url: form_url, method: form_method do |f| %>
32
+ <% form.fields.each do |field| %>
33
+ <div>
34
+ <% unless field.input == Neewom::AbstractField::SUBMIT %>
35
+ <%= f.label field.name, field.label %>
36
+ <% end %>
37
+ <% case field.input %>
38
+ <% when Neewom::AbstractField::EMAIL %>
39
+ <%= f.email_field field.name, field.input_html %>
40
+ <% when Neewom::AbstractField::HIDDEN %>
41
+ <%= f.hidden_field field.name, field.input_html %>
42
+ <% when Neewom::AbstractField::NUMBER %>
43
+ <%= f.number_field field.name, field.input_html %>
44
+ <% when Neewom::AbstractField::PASSWORD %>
45
+ <%= f.password_field field.name, field.input_html %>
46
+ <% when Neewom::AbstractField::PHONE %>
47
+ <%= f.phone_field field.name, field.input_html %>
48
+ <% when Neewom::AbstractField::SELECT %>
49
+ <% if field.collection.present? %>
50
+ <% collection = field.collection %>
51
+ <% else %>
52
+ <% method_params = field.collection_params.map { |method_name| public_send(method_name) } %>
53
+ <% collection = field.collection_klass.constintize.public_send(field.collection_method, *method_params) %>
54
+ <% end %>
55
+
56
+ <% options = collection.map { |i| [i.public_send(field.label_method), i.public_send(field.value_method)] } %>
57
+
58
+ <%= f.select field.name, options, field.input_html %>
59
+ <% when Neewom::AbstractField::SUBMIT %>
60
+ <%= f.submit field.label, {name: field.name}.merge(field.input_html) %>
61
+ <% when Neewom::AbstractField::TEXTAREA %>
62
+ <%= f.text_area field.name, field.input_html %>
63
+ <% when Neewom::AbstractField::TEXT %>
64
+ <%= f.text_field field.name, field.input_html %>
65
+ <% end %>
66
+ <% if @resource.errors[field.name].any? %>
67
+ <span class="errors"><%= @resource.errors[field.name].join(', ')%></span>
68
+ <% end %>
69
+ </div>
70
+ <% end %>
71
+ <% end %>
72
+ ```
73
+
74
+ ## Usage
75
+
76
+ Add a jsonb field, which will store the custom attributes
77
+
78
+ ```ruby
79
+ class AddAttributesToUsers < ActiveRecord::Migration[5.1]
80
+ def change
81
+ add_column :users, :data, :jsonb
82
+ end
83
+ end
84
+ ```
85
+
86
+ Configure your model to work with that field
87
+
88
+ ```ruby
89
+ class User < ApplicationRecord
90
+ include Neewom::Model
91
+
92
+ has_neewom_attributes :data
93
+ end
94
+ ```
95
+
96
+ Next you need to describe the form. Please note, that by default you need to describe all fields, not just a custom ones. But, you can generate another template and predefine some fields there.
97
+
98
+ ```ruby
99
+ Neewom::AbstractForm.build(
100
+ id: :custom_user_form,
101
+ repository_klass: 'User',
102
+ fields: {
103
+ name: {
104
+ virtual: true
105
+ },
106
+ email: {
107
+ virtual: false,
108
+ input: 'email_field'
109
+ },
110
+ password: {
111
+ virtual: false,
112
+ input: 'password_field',
113
+ validations: {presence: true, confirmation: true}
114
+ },
115
+ password_confirmation: {
116
+ virtual: false,
117
+ input: 'password_field'
118
+ },
119
+ commit: {
120
+ label: 'Save',
121
+ input: 'submit'
122
+ }
123
+ }
124
+ )
125
+ ```
126
+
127
+ ### Form attributes
128
+
129
+ 1. **id** (*required*) - An unique form id
130
+ 2. **repository_klass** (*required*) - An active record model name with configured neewom attributes
131
+ 3. **fields** (*required*) - A hash with fields config.
132
+ 4. **template** - form template name ("form" by default)
133
+
134
+ ### Field attrbutes
135
+
136
+ The hash keys are also a names.
137
+
138
+ 1. **label** - Input label. By default it's `name.to_s.humanize`
139
+ 2. **input** - Field input. By default it's 'text_field'. Check the `Neewom::AbstractField::SUPPORTED_FIELDS` to get the list of supported inputs
140
+ 3. **virtual** - Boolean, true by default. Should be false if you need to store data in a real column instead of the jsonb one
141
+ 4. **validations** - Hash, by default an empty one. Should be the standard rails validations
142
+ 5. **collection** - Collection of objects for the select input. Can not be stored to the database
143
+ 6. **label_method** - A label method for collection. Used while building options for select
144
+ 7. **value_method** - A value method for collection. Used while building options for select. It's 'id' by default
145
+ 8. **input_html** - A hash with the HTML attributes
146
+
147
+ Another way to define a collection is to pass three params
148
+
149
+ 1. **collection_klass** - A class which contain the specific logic
150
+ 2. **collection_method** - A class method of the `collection_class`
151
+ 3. **collection_params** - A view context methods. Will pass to `collection_method`
152
+
153
+ So, the `{collection_klass: 'EmployeesCollections', collection_method: 'managers_for_user', collection_params: [:current_user]}` will call the
154
+
155
+ ```ruby
156
+ EmployeesCollections.managers_for_user(current_user)
157
+ ```
158
+
159
+ inside the view.
160
+
161
+ ## Defining a controller
162
+
163
+ You need to define the next methods as a helper_methods: `form_url`, `form_method`, `form`
164
+
165
+ An instance of the `Neewom::AbstractForm` will have a set of usefull methods you need to use in the controller
166
+
167
+ 1. `form.build_resource(permitted_params)` - will build a new ActiveRecord model
168
+ 2. `form.find(id)`
169
+ 3. `form.find_by(id: 1)`
170
+ 4. `form.find_by!(id: 1)`
171
+ 5. `form.repository_klass.constantize` - get a ActiveRecord model class
172
+ 6. `form.strong_params_require` - the require part for strong params
173
+ 7. `form.strong_params_permit` - the permit part for strong params
174
+
175
+
176
+ It's more easy to use an existing methods, because there are an existing neewom initialization inside
177
+
178
+ ```ruby
179
+ def build_resource(params)
180
+ resource = repository_klass.constantize.new
181
+ resource.initialize_neewom_attributes(self)
182
+ resource.assign_attributes(params) if params.present?
183
+
184
+ resource
185
+ end
186
+ ```
187
+
188
+ ### The complete controller example
189
+
190
+ ```ruby
191
+ class CustomController < ApplicationController
192
+ def new
193
+ @resource = form.build_resource
194
+ render "neewom_forms/#{form.template}"
195
+ end
196
+
197
+ def create
198
+ @resource = form.build_resource permitted_params
199
+
200
+ if @resource.save
201
+ redirect_to root_path
202
+ else
203
+ render "neewom_forms/#{form.template}"
204
+ end
205
+ end
206
+
207
+ private
208
+
209
+ def permitted_params
210
+ params.require(form.strong_params_require).permit(form.strong_params_permit)
211
+ end
212
+
213
+ def form_url
214
+ custom_index_path
215
+ end
216
+ helper_method :form_url
217
+
218
+ def form_method
219
+ :post
220
+ end
221
+ helper_method :form_method
222
+
223
+ def form
224
+ @form ||= Neewom::AbstractForm.build(
225
+ id: :custom_user_form,
226
+ repository_klass: 'User',
227
+ fields: {
228
+ name: {
229
+ virtual: true
230
+ },
231
+ email: {
232
+ virtual: false,
233
+ input: 'email_field'
234
+ },
235
+ password: {
236
+ virtual: false,
237
+ input: 'password_field',
238
+ validations: {presence: true, confirmation: true}
239
+ },
240
+ password_confirmation: {
241
+ virtual: false,
242
+ input: 'password_field'
243
+ },
244
+ commit: {
245
+ label: 'Save',
246
+ input: 'submit'
247
+ }
248
+ }
249
+ )
250
+ end
251
+ helper_method :form
252
+ end
253
+ ```
254
+
255
+ ## Storing forms in the database.
256
+
257
+ If you didn't used a `collection` in any field, you can store the form to the database.
258
+
259
+ Add a neewom tables first
260
+
261
+ ```ruby
262
+ def change
263
+ create_table :neewom_forms do |t|
264
+ t.string :key, null: false, index: { unique: true }
265
+ t.string :description
266
+ t.string :crc32, null: false, index: { unique: true }
267
+ t.string :repository_klass, null: false
268
+ t.string :template, null: false
269
+
270
+ t.timestamps null: false
271
+ end
272
+
273
+ create_table :neewom_fields do |t|
274
+ t.integer :form_id, null: false
275
+ t.string :label
276
+ t.string :name, null: false
277
+ t.string :input
278
+ t.boolean :virtual
279
+ t.string :validations
280
+ t.string :collection_klass
281
+ t.string :collection_method
282
+ t.string :collection_params
283
+ t.string :label_method
284
+ t.string :value_method
285
+ t.string :input_html
286
+
287
+ t.timestamps null: false
288
+ end
289
+
290
+ add_index :neewom_fields, [:form_id, :name], unique: true
291
+ end
292
+ ```
293
+
294
+ Then you can store and fetch forms.
295
+
296
+ ```ruby
297
+
298
+ form.store!
299
+ restored_form = Neewom::CustomForm.find_by!(key: form.id).to_form
300
+ ```
301
+
302
+ ## Development
303
+
304
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
305
+
306
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
307
+
308
+ ## Contributing
309
+
310
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/neewom. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
311
+
312
+ ## License
313
+
314
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
315
+
316
+ ## Code of Conduct
317
+
318
+ Everyone interacting in the Neewom project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/neewom/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'rails/dummy/tasks'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "neewom"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,68 @@
1
+ module Neewom
2
+ class AbstractField
3
+ # This constant can be changed OUTSIDE !!!
4
+ SUPPORTED_FIELDS = [
5
+ # 'check_box_collection',
6
+ EMAIL = 'email_field',
7
+ HIDDEN = 'hidden_field',
8
+ NUMBER = 'number_field',
9
+ PASSWORD = 'password_field',
10
+ PHONE = 'phone_field',
11
+ # 'radio_button_collection',
12
+ SELECT = 'select_field',
13
+ MULTIPLE_SELECT = 'multiple_select_field',
14
+ SUBMIT = 'submit',
15
+ TEXTAREA = 'text_area',
16
+ TEXT = 'text_field',
17
+ CHECKBOX = 'checkbox',
18
+ FILE = 'file',
19
+ DATEPICKER = 'datepicker',
20
+ TIMEPICKER = 'timepicker',
21
+ DATETIMEPICKER = 'datetimepicker'
22
+ ]
23
+
24
+ attr_accessor :label, :name, :input, :virtual, :validations,
25
+ :collection, :collection_klass, :collection_method, :collection_params,
26
+ :label_method, :value_method, :input_html, :custom_options
27
+
28
+ def submit?
29
+ input == SUBMIT
30
+ end
31
+
32
+ def label
33
+ @label || name.to_s.humanize
34
+ end
35
+
36
+ def input
37
+ @input || 'text_field'
38
+ end
39
+
40
+ def virtual
41
+ @virtual.nil? ? true : @virtual
42
+ end
43
+
44
+ def input_html
45
+ @input_html || {}
46
+ end
47
+
48
+ def validations
49
+ @validations || {}
50
+ end
51
+
52
+ def custom_options
53
+ @custom_options || {}
54
+ end
55
+
56
+ def build_validations
57
+ validations
58
+ end
59
+
60
+ def label_method
61
+ @label_method || :name
62
+ end
63
+
64
+ def value_method
65
+ @value_method || :id
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,65 @@
1
+ module Neewom
2
+ class AbstractForm
3
+ include Neewom::Proxies::BuildersAndFinders
4
+
5
+ include Neewom::Serializer
6
+
7
+ attr_accessor :id, :repository_klass, :template, :fields, :persist_submit_controls
8
+
9
+ def self.build(config)
10
+ form = self.new
11
+ form.id = config[:id]
12
+ form.repository_klass = config[:repository_klass]
13
+ form.template = config[:template]
14
+ form.persist_submit_controls = config[:persist_submit_controls]
15
+
16
+ form.fields = config[:fields].map do |name, field_config|
17
+ field = Neewom::AbstractField.new
18
+
19
+ field_config = { input: field_config } if field_config.is_a?(String)
20
+
21
+ field.name = name
22
+ field.label = field_config[:label]
23
+ field.input = field_config[:input]
24
+ field.virtual = field_config[:virtual]
25
+ field.validations = field_config[:validations]
26
+ field.collection = field_config[:collection]
27
+ field.collection_klass = field_config[:collection_klass]
28
+ field.collection_method = field_config[:collection_method]
29
+ field.collection_params = field_config[:collection_params]
30
+ field.label_method = field_config[:label_method]
31
+ field.value_method = field_config[:value_method]
32
+ field.input_html = field_config[:input_html]
33
+ field.custom_options = field_config[:custom_options]
34
+
35
+ field
36
+ end
37
+
38
+ form
39
+ end
40
+
41
+ def fields
42
+ @fields || []
43
+ end
44
+
45
+ def template
46
+ @template || 'form'
47
+ end
48
+
49
+ def virtual_fields
50
+ fields.select(&:virtual)
51
+ end
52
+
53
+ def submit_fields
54
+ fields.select(&:submit?)
55
+ end
56
+
57
+ def persisted_fields
58
+ persist_submit_controls ? fields : fields.reject(&:submit?)
59
+ end
60
+
61
+ def store!
62
+ Neewom::Repository.new.store!(self)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ module Neewom
2
+ class CustomField < ActiveRecord::Base
3
+ self.table_name = 'neewom_fields'
4
+
5
+ belongs_to :custom_form, foreign_key: :form_id
6
+
7
+ validates :name, presence: true
8
+ validates :input, inclusion: { in: Neewom::AbstractField::SUPPORTED_FIELDS }
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ module Neewom
2
+ class CustomForm < ActiveRecord::Base
3
+ self.table_name = 'neewom_forms'
4
+
5
+ has_many :custom_fields, -> { order(order: :asc) }, foreign_key: :form_id
6
+
7
+ validates :key, :repository_klass, :template, presence: true
8
+ validates :description, length: {minimum: 3, maximum: 255}, allow_blank: true
9
+
10
+ def to_form
11
+ Neewom::Repository.new.fetch!(self)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,51 @@
1
+ module Neewom
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_accessor :neewom_form
7
+
8
+ def initialize_neewom_attributes(key_or_form)
9
+ data_column = self.class.neewom_attributes_column
10
+
11
+ self.neewom_form =
12
+ case key_or_form
13
+ when Neewom::AbstractForm
14
+ key_or_form
15
+ when Neewom::CustomForm
16
+ key_or_form.to_form
17
+ else
18
+ Neewom::CustomForm.find_by!(key: key_or_form).to_form
19
+ end
20
+
21
+ neewom_form.virtual_fields.map do |field|
22
+ name = field.name.to_sym
23
+
24
+ singleton_class.class_eval do
25
+ store_accessor data_column, name
26
+ end
27
+ end
28
+
29
+ neewom_form.fields.map do |field|
30
+ name = field.name.to_sym
31
+ validations_config = field.validations
32
+
33
+ singleton_class.class_eval do
34
+ if validations_config.present?
35
+ validates [name, validations_config].flatten
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+ attr_accessor :neewom_attributes_column
44
+
45
+ def has_neewom_attributes(column)
46
+ self.neewom_attributes_column = column
47
+ serialize column, Hash
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,87 @@
1
+ module Neewom
2
+ module Proxies
3
+ module BuildersAndFinders
4
+ def build_resource(controller_params=nil, initial_values: {})
5
+ resource = repository_klass.constantize.new
6
+ apply_inputs(resource, controller_params, initial_values: initial_values)
7
+ end
8
+
9
+ def apply_inputs(resource, controller_params, initial_values: {})
10
+ return unless resource.present?
11
+
12
+ resource.initialize_neewom_attributes(self) if resource.respond_to?(:initialize_neewom_attributes)
13
+
14
+ if controller_params.present?
15
+ params = permit_params(controller_params)
16
+
17
+ initial_values.each do |field, value|
18
+ if resource.respond_to?(:"#{field}=")
19
+ resource.public_send(:"#{field}=", value)
20
+ else
21
+ raise "The form trying to setup the #{field} field from an initial data, but the #{resource.class.name} can't accept it"
22
+ end
23
+ end
24
+
25
+ data = persisted_fields.each_with_object({}) do |field, acc|
26
+ acc[field.name.to_sym] = params[field.name.to_sym] if params.has_key?(field.name.to_sym)
27
+ end
28
+
29
+ resource.assign_attributes(data)
30
+ end
31
+
32
+ resource
33
+ end
34
+
35
+ def parse_submit_control_value(controller_params)
36
+ submit_fields.each_with_object({}) do |field, acc|
37
+ acc[field.name.to_sym] = controller_params[field.name.to_sym] if controller_params.has_key?(field.name.to_sym)
38
+ end
39
+ end
40
+
41
+ def find(id)
42
+ resource = repository_klass.constantize.find(id)
43
+ resource.initialize_neewom_attributes(self)
44
+
45
+ resource
46
+ end
47
+
48
+ def find_by(options)
49
+ resource = repository_klass.constantize.find_by(options)
50
+ resource.initialize_neewom_attributes(self) if resource.present?
51
+
52
+ resource
53
+ end
54
+
55
+ def find_by!(options)
56
+ resource = repository_klass.constantize.find_by!(options)
57
+ resource.initialize_neewom_attributes(self)
58
+
59
+ resource
60
+ end
61
+
62
+ def find_and_apply_inputs(id, form_params, initial_values: {})
63
+ apply_inputs(find(id), form_params, initial_values: initial_values)
64
+ end
65
+
66
+ def find_by_and_apply_inputs(options, form_params, initial_values: {})
67
+ apply_inputs(find_by(options), form_params, initial_values: initial_values)
68
+ end
69
+
70
+ def find_by_and_apply_inputs!(options, form_params, initial_values: {})
71
+ apply_inputs(find_by!(options), form_params, initial_values: initial_values)
72
+ end
73
+
74
+ def permit_params(controller_params)
75
+ controller_params.require(strong_params_require).permit(strong_params_permit)
76
+ end
77
+
78
+ def strong_params_require
79
+ repository_klass.constantize.model_name.param_key.to_sym
80
+ end
81
+
82
+ def strong_params_permit
83
+ fields.map(&:name)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,110 @@
1
+ require 'zlib'
2
+
3
+ module Neewom
4
+ class Repository
5
+ def store!(abstract_form)
6
+ form_record = Neewom::CustomForm.find_by key: abstract_form.id
7
+ current_crc = calculate_crc32(abstract_form)
8
+
9
+ return if form_record.present? && form_record.crc32 == current_crc
10
+
11
+ form_record ||= Neewom::CustomForm.new(key: abstract_form.id)
12
+
13
+ form_record.transaction do
14
+ form_record.assign_attributes(
15
+ repository_klass: abstract_form.repository_klass,
16
+ template: abstract_form.template,
17
+ crc32: current_crc,
18
+ persist_submit_controls: (abstract_form.persist_submit_controls || false)
19
+ )
20
+ existing_fields = form_record.custom_fields.to_a
21
+
22
+ abstract_form.fields.each_with_index do |field, index|
23
+ if field.collection.present?
24
+ raise "Form with specified collection could not be stored."
25
+ end
26
+
27
+ field_record = existing_fields.find { |item| item.name.to_s == field.name.to_s }
28
+ field_record ||= Neewom::CustomField.new(custom_form: form_record, name: field.name)
29
+
30
+ field_record.update!(
31
+ order: index,
32
+ label: field.label,
33
+ input: field.input,
34
+ virtual: field.virtual,
35
+ validations: field.validations.to_json,
36
+ collection_klass: field.collection_klass,
37
+ collection_method: field.collection_method,
38
+ collection_params: field.collection_params.to_json,
39
+ label_method: field.label_method,
40
+ value_method: field.value_method,
41
+ input_html: field.input_html.to_json,
42
+ custom_options: field.custom_options.to_json
43
+ )
44
+
45
+ form_record.custom_fields << field_record
46
+ end
47
+
48
+ removable_fields = existing_fields.select { |item| abstract_form.fields.none? { |f| f.name.to_s == item.name.to_s } }
49
+ removable_fields.each(&:destroy)
50
+
51
+ form_record.save!
52
+ end
53
+
54
+ form_record
55
+ end
56
+
57
+ def fetch!(form_record)
58
+ form = Neewom::AbstractForm.new
59
+ form.id = form_record.key
60
+ form.repository_klass = form_record.repository_klass
61
+ form.template = form_record.template
62
+ form.persist_submit_controls = form_record.persist_submit_controls
63
+
64
+ form.fields = form_record.custom_fields.map do |field_record|
65
+ field = Neewom::AbstractField.new
66
+ field.label = field_record.label
67
+ field.name = field_record.name
68
+ field.input = field_record.input
69
+ field.virtual = field_record.virtual
70
+ field.validations = JSON.parse(field_record.validations, symbolize_names: true)
71
+ field.collection_klass = field_record.collection_klass
72
+ field.collection_method = field_record.collection_method
73
+ field.collection_params = JSON.parse(field_record.collection_params)&.map(&:to_sym)
74
+ field.label_method = field_record.label_method
75
+ field.value_method = field_record.value_method
76
+ field.input_html = JSON.parse(field_record.input_html, symbolize_names: true)
77
+ field.custom_options = JSON.parse(field_record.custom_options, symbolize_names: true)
78
+
79
+ field
80
+ end
81
+
82
+ form
83
+ end
84
+
85
+ private
86
+
87
+ def calculate_crc32(abstract_form)
88
+ buff = [abstract_form.id, abstract_form.repository_klass, abstract_form.template, abstract_form.persist_submit_controls].join(':')
89
+
90
+ buff += abstract_form.fields.map do |field|
91
+ [
92
+ field.label,
93
+ field.name,
94
+ field.input,
95
+ field.virtual,
96
+ field.validations.to_json,
97
+ field.collection_klass,
98
+ field.collection_method,
99
+ field.collection_params.to_json,
100
+ field.label_method,
101
+ field.value_method,
102
+ field.input_html.to_json,
103
+ field.custom_options.to_json
104
+ ].map(&:to_s)
105
+ end.flatten.join(':')
106
+
107
+ Zlib::crc32(buff).to_s
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,38 @@
1
+ require 'zlib'
2
+
3
+ module Neewom
4
+ module Serializer
5
+ # It looks like it may have sense to store an input config and return it.
6
+ # However theare some fields are not serializable and also there are default values.
7
+
8
+ def to_h
9
+ result = {}
10
+ result[:id] = id
11
+ result[:repository_klass] = repository_klass
12
+ result[:template] = template
13
+ result[:fields] = {}
14
+
15
+ fields.each do |field|
16
+ field_data = {}
17
+ field_data[:label] = field.label
18
+ field_data[:input] = field.input
19
+ field_data[:validations] = field.validations
20
+ field_data[:collection_klass] = field.collection_klass
21
+ field_data[:collection_method] = field.collection_method
22
+ field_data[:collection_params] = field.collection_params
23
+ field_data[:label_method] = field.label_method
24
+ field_data[:value_method] = field.value_method
25
+ field_data[:input_html] = field.input_html
26
+ field_data[:custom_options] = field.custom_options
27
+
28
+ result[:fields][field.name.to_sym] = field_data
29
+ end
30
+
31
+ result
32
+ end
33
+
34
+ def to_json
35
+ to_hash.to_json
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Neewom
2
+ VERSION = "0.1.0"
3
+ end
data/lib/neewom.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "neewom/version"
2
+
3
+ require "neewom/proxies/builders_and_finders"
4
+ require "neewom/serializer"
5
+ require "neewom/abstract_field"
6
+ require "neewom/abstract_form"
7
+ require "neewom/custom_field"
8
+ require "neewom/custom_form"
9
+ require "neewom/model"
10
+ require "neewom/repository"
11
+
12
+
13
+ module Neewom
14
+ class Error < StandardError; end
15
+ # Your code goes here...
16
+ end
data/neewom.gemspec ADDED
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "neewom/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "neewom"
8
+ spec.version = Neewom::VERSION
9
+ spec.authors = ["Oleg Zaporozhchenko"]
10
+ spec.email = ["olegz@jetruby.com"]
11
+
12
+ spec.summary = %q{Rails ActiveRecord custom fields and forms based on Postgres json fields}
13
+ spec.description = %q{The most flexible solution for organizing custom fields for Rails ActiveRecord}
14
+ spec.homepage = "https://github.com/trueknightwhosayni/neewom"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/trueknightwhosayni/neewom"
24
+ spec.metadata["changelog_uri"] = "https://github.com/trueknightwhosayni/neewom/blob/master/CHANGELOG.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neewom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Zaporozhchenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: The most flexible solution for organizing custom fields for Rails ActiveRecord
56
+ email:
57
+ - olegz@jetruby.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/neewom.rb
74
+ - lib/neewom/abstract_field.rb
75
+ - lib/neewom/abstract_form.rb
76
+ - lib/neewom/custom_field.rb
77
+ - lib/neewom/custom_form.rb
78
+ - lib/neewom/model.rb
79
+ - lib/neewom/proxies/builders_and_finders.rb
80
+ - lib/neewom/repository.rb
81
+ - lib/neewom/serializer.rb
82
+ - lib/neewom/version.rb
83
+ - neewom.gemspec
84
+ homepage: https://github.com/trueknightwhosayni/neewom
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ homepage_uri: https://github.com/trueknightwhosayni/neewom
89
+ source_code_uri: https://github.com/trueknightwhosayni/neewom
90
+ changelog_uri: https://github.com/trueknightwhosayni/neewom/blob/master/CHANGELOG.md
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.0.4
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Rails ActiveRecord custom fields and forms based on Postgres json fields
110
+ test_files: []