schemafier 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: cda3414b105af8509ea6ca821770e17b742bcd19
4
- data.tar.gz: '068e94a2dcf64974120bdb36116d41242fce9196'
3
+ metadata.gz: aaf434a76901b1883aeca0bddddc8ae7f12c113c
4
+ data.tar.gz: 45c93846af184d1e387f7b8bc9b41025bf6e8438
5
5
  SHA512:
6
- metadata.gz: '038d8ad7bf066bb516a100c5ed53db4943b1522aeefa6f5793514f7b0bbc0b24e802b568e6933be0f3a351bc9f75d7e70ac0b52fa09ea03d5da01f128d4f78f3'
7
- data.tar.gz: 0a168ac6d7fe506fa3584cd3751b73d8636ba298dfacd0a9eafa3fcae63b2745170b39977f834e7f894cbc8c34755a83cd2e03cfa3745307c3602bb71d4fd815
6
+ metadata.gz: c063345cfd538200a93eccc1cc49fcf77768c543b83635fbf545f32970fb407d1fbe87f26ae716e0e52ce7a32cf3e54f89f57f97436e0b3af509c38071a3f8b9
7
+ data.tar.gz: 20b94c719fae08935c225ced9f88c65b3fa39d404a048a00c726f9e5a8f726e4344863cad0e61c34f2b0d48753ba408c0a76bc882a39c7769f3258e99cff5330
@@ -1,3 +1,3 @@
1
1
  module Schemafier
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/schemafier.rb CHANGED
@@ -1,12 +1,117 @@
1
- module Bowtie
2
- module Schemafier
3
- module Controller
4
- module Concerns
1
+ module Schemafier
2
+ extend ActiveSupport::Concern
3
+
4
+ # Ensure STEPS Hash keys are all symbols
5
+ def clean_steps
6
+ Hash[steps.map { |key, val| [key.to_sym, val] } ]
7
+ end
8
+
9
+ # Return all step keys (step names)
10
+ def step_keys
11
+ clean_steps.keys
12
+ end
13
+
14
+ # Return the name of the first step
15
+ def first_step
16
+ name, fields = clean_steps.first
17
+
18
+ name
19
+ end
20
+
21
+ # Return the name of the last step
22
+ def last_step
23
+ name, fields = clean_steps.last
24
+
25
+ name
26
+ end
27
+
28
+ # Return the name of the previous step (if past first step)
29
+ def prev_step
30
+ if current_step_index > 0
31
+ step_keys[current_step_index - 1]
32
+ end
33
+ end
34
+
35
+ # Return the name of the next step (if there are steps remaining)
36
+ def next_step
37
+ if current_step_index < step_keys.length - 1
38
+ step_keys[current_step_index + 1]
39
+ end
40
+ end
41
+
42
+ # Obtain the current step name from :id parameter in route path
43
+ def current_step
44
+ params.include?(:id) ? params[:id].to_sym : first_step
45
+ end
46
+
47
+ # Find index of the current step (lookup in step_keys)
48
+ def current_step_index
49
+ step_keys.index(current_step)
50
+ end
51
+
52
+ # Get fields for a step (default step to current_step)
53
+ def get_step_fields(step = nil)
54
+ step ||= current_step
55
+
56
+ # Get schema for current model class
57
+ schema = get_step_subject_schema
58
+
59
+ # Get the fields for the current step
60
+ fields = clean_steps[step.to_sym]
61
+
62
+ # Respond with the schema for current step fields
63
+ schema.select { |key, val| fields.include?(key.to_sym) }
64
+ end
65
+
66
+ # Lookup the schema of all fields for the step subject model
67
+ def get_step_subject_schema
68
+ # Get the model from the subject (class definition, not instance)
69
+ model = @step_subject.class
70
+
71
+ # Find all database fields (except id)
72
+ fields = model.columns_hash.select { |key, value| key.to_s != "id" }
73
+
74
+ # Return reconstructed hash of model schema
75
+ Hash[fields.map { |key, value|
76
+ enum_method = key.pluralize.to_sym
77
+
78
+ if model.respond_to? enum_method
79
+ [key, model.send(enum_method).keys]
80
+ else
81
+ [key, value.type]
5
82
  end
83
+ } ]
84
+ end
85
+
86
+ # Render json step response (for current_step)
87
+ def render_step_response
88
+ response = {
89
+ index: current_step_index,
90
+ total: step_keys.length,
91
+ name: current_step,
92
+ fields: get_step_fields
93
+ }
94
+
95
+ response[:prev] = prev_step if prev_step
96
+ response[:next] = next_step if next_step
97
+
98
+ render json: response
99
+ end
100
+
101
+ # Update the step subject (model) with params from current_step
102
+ def update_step_subject
103
+ if @step_subject.update(update_params)
104
+ ap "UPDATED!"
105
+ else
106
+ ap "FAILED TO UPDATE!"
107
+ ap @step_subject
6
108
  end
109
+
110
+ render_step_response
7
111
  end
8
- # autoload :WizardController, "bowtie/wizard_controller"
9
- end
10
112
 
11
- require 'schemafier/controller/concerns/steps'
12
- require 'schemafier/schemafier'
113
+ # Define step params permitted as only those defined in the specified step
114
+ def update_params(step = nil)
115
+ params.permit(get_step_fields(step).keys)
116
+ end
117
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemafier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie McClung
@@ -20,8 +20,6 @@ extra_rdoc_files:
20
20
  files:
21
21
  - README.md
22
22
  - lib/schemafier.rb
23
- - lib/schemafier/controllers/concerns/steps.rb
24
- - lib/schemafier/schemafier.rb
25
23
  - lib/schemafier/version.rb
26
24
  - schemafier.gemspec
27
25
  homepage: https://github.com/bowtie-co/ruby-schemafier
@@ -1,117 +0,0 @@
1
- module Bowtie::Schemafier::Controller::Concerns::Steps
2
- extend ActiveSupport::Concern
3
-
4
- # Ensure STEPS Hash keys are all symbols
5
- def clean_steps
6
- Hash[steps.map { |key, val| [key.to_sym, val] } ]
7
- end
8
-
9
- # Return all step keys (step names)
10
- def step_keys
11
- clean_steps.keys
12
- end
13
-
14
- # Return the name of the first step
15
- def first_step
16
- name, fields = clean_steps.first
17
-
18
- name
19
- end
20
-
21
- # Return the name of the last step
22
- def last_step
23
- name, fields = clean_steps.last
24
-
25
- name
26
- end
27
-
28
- # Return the name of the previous step (if past first step)
29
- def prev_step
30
- if current_step_index > 0
31
- step_keys[current_step_index - 1]
32
- end
33
- end
34
-
35
- # Return the name of the next step (if there are steps remaining)
36
- def next_step
37
- if current_step_index < step_keys.length - 1
38
- step_keys[current_step_index + 1]
39
- end
40
- end
41
-
42
- # Obtain the current step name from :id parameter in route path
43
- def current_step
44
- params.include?(:id) ? params[:id].to_sym : first_step
45
- end
46
-
47
- # Find index of the current step (lookup in step_keys)
48
- def current_step_index
49
- step_keys.index(current_step)
50
- end
51
-
52
- # Get fields for a step (default step to current_step)
53
- def get_step_fields(step = nil)
54
- step ||= current_step
55
-
56
- # Get schema for current model class
57
- schema = get_step_subject_schema
58
-
59
- # Get the fields for the current step
60
- fields = clean_steps[step.to_sym]
61
-
62
- # Respond with the schema for current step fields
63
- schema.select { |key, val| fields.include?(key.to_sym) }
64
- end
65
-
66
- # Lookup the schema of all fields for the step subject model
67
- def get_step_subject_schema
68
- # Get the model from the subject (class definition, not instance)
69
- model = @step_subject.class
70
-
71
- # Find all database fields (except id)
72
- fields = model.columns_hash.select { |key, value| key.to_s != "id" }
73
-
74
- # Return reconstructed hash of model schema
75
- Hash[fields.map { |key, value|
76
- enum_method = key.pluralize.to_sym
77
-
78
- if model.respond_to? enum_method
79
- [key, model.send(enum_method).keys]
80
- else
81
- [key, value.type]
82
- end
83
- } ]
84
- end
85
-
86
- # Render json step response (for current_step)
87
- def render_step_response
88
- response = {
89
- index: current_step_index,
90
- total: step_keys.length,
91
- name: current_step,
92
- fields: get_step_fields
93
- }
94
-
95
- response[:prev] = prev_step if prev_step
96
- response[:next] = next_step if next_step
97
-
98
- render json: response
99
- end
100
-
101
- # Update the step subject (model) with params from current_step
102
- def update_step_subject
103
- if @step_subject.update(update_params)
104
- ap "UPDATED!"
105
- else
106
- ap "FAILED TO UPDATE!"
107
- ap @step_subject
108
- end
109
-
110
- render_step_response
111
- end
112
-
113
- # Define step params permitted as only those defined in the specified step
114
- def update_params(step = nil)
115
- params.permit(get_step_fields(step).keys)
116
- end
117
- end
@@ -1,7 +0,0 @@
1
- module Bowtie
2
- module Schemafier
3
- extend ActiveSupport::Concern
4
-
5
- include Bowtie::Schemafier::Controller::Concerns::Steps
6
- end
7
- end