sequent-sinatra 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sequent-sinatra/app.rb +8 -2
- data/lib/sequent-sinatra/fieldset.rb +16 -2
- data/lib/sequent-sinatra/form_helpers.rb +28 -0
- data/lib/sequent-sinatra/tag_helper.rb +56 -3
- data/lib/version.rb +1 -1
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff3009b374b8b75cad6de45571b76487c92a691d
|
4
|
+
data.tar.gz: 8fa9e461c0c80a3f7b2610c8af6eeb915287165e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eecf5a4b6e328bad1282f225248d01b1a7c239cae5b55e6b917ae4feb824845b234283af491765cfec2bece2b85c63eca2d7611f61cb9cc0a875dcff05eae175
|
7
|
+
data.tar.gz: 4ce04e459bde7fe61d229495795e2595b135b67821dfda16a426fa0c4c50f71b57796419679f2faee1a03c226380103351002a28bd9f73fad4ade20fa6fc2951
|
data/lib/sequent-sinatra/app.rb
CHANGED
@@ -21,10 +21,16 @@ module Sequent
|
|
21
21
|
app.helpers Sequent::Core::Helpers::UuidHelper
|
22
22
|
app.helpers Sequent::Web::Sinatra::FormHelpers
|
23
23
|
app.helpers Sequent::Web::Sinatra::SimpleCommandServiceHelpers
|
24
|
+
app.set :sequent_config_dir, app.root unless app.respond_to?(:sequent_config_dir)
|
24
25
|
|
25
26
|
app.before do
|
26
|
-
|
27
|
-
|
27
|
+
config_file = File.join(app.sequent_config_dir, 'initializers/sequent')
|
28
|
+
if File.exist?("#{config_file}.rb") || File.exist?("#{config_file}")
|
29
|
+
require config_file
|
30
|
+
else
|
31
|
+
raise "Unable to initialize Sequent. Config file #{config_file} not found.\nInitialize Sequent correctly? First set the 'sequent_config_dir' or the 'root', then register Sequent::Web::Sinatra in your Sinatra application"
|
32
|
+
end
|
33
|
+
@command_service = Sequent.command_service
|
28
34
|
end
|
29
35
|
|
30
36
|
end
|
@@ -9,15 +9,25 @@ module Sequent
|
|
9
9
|
|
10
10
|
attr_reader :path, :parent
|
11
11
|
|
12
|
+
def postfix
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
12
16
|
def initialize(parent, path, params, errors, options = {})
|
13
17
|
raise "params are empty while creating new fieldset path #{path}" unless params
|
14
|
-
@values = params
|
18
|
+
@values = params[path] || {}
|
15
19
|
@parent = parent
|
16
20
|
@path = path.to_s.gsub(/\W+/, '')
|
17
21
|
@errors = errors
|
18
22
|
@options = options
|
19
23
|
end
|
20
24
|
|
25
|
+
def nested_array(name)
|
26
|
+
(@values[name] || [{}]).each do |value|
|
27
|
+
yield FieldsetInArray.new(self, name, { name => value }, @errors, @options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
21
31
|
def nested(name)
|
22
32
|
yield Fieldset.new(self, name, @values, @errors, @options)
|
23
33
|
end
|
@@ -29,9 +39,13 @@ module Sequent
|
|
29
39
|
def path_for(field_name)
|
30
40
|
css_id @path, field_name
|
31
41
|
end
|
32
|
-
|
33
42
|
end
|
34
43
|
|
44
|
+
class FieldsetInArray < Fieldset
|
45
|
+
def postfix
|
46
|
+
"[]"
|
47
|
+
end
|
48
|
+
end
|
35
49
|
end
|
36
50
|
end
|
37
51
|
end
|
@@ -4,21 +4,49 @@ require 'rack/csrf'
|
|
4
4
|
module Sequent
|
5
5
|
module Web
|
6
6
|
module Sinatra
|
7
|
+
##
|
8
|
+
# Various form helpers to help bind html forms to `Sequent::Core::Command`s
|
9
|
+
#
|
7
10
|
module FormHelpers
|
11
|
+
##
|
12
|
+
# Creates a form not bound to any Command
|
13
|
+
#
|
14
|
+
# Parameters
|
15
|
+
# +action+ the action attribute of the <form> tag
|
16
|
+
# +method+ the method attribute of the <form> tag.
|
17
|
+
# :delete and :update will result in an extra hidden field _method to be set so sinatra will call the corresponding `delete '/' {}` or `update `/` {}` methods in your application.
|
18
|
+
# +options+ options hash
|
19
|
+
#
|
8
20
|
def html_form(action, method=:get, options={}, &block)
|
9
21
|
html_form_for nil, action, method, options, &block
|
10
22
|
end
|
11
23
|
|
24
|
+
##
|
25
|
+
# Creates a for bound to an object that respond_to :as_params. This is typically a `Sequent::Core::Command` or a `Sequent::Core::ValueObject`.
|
26
|
+
#
|
27
|
+
# Parameters
|
28
|
+
# +for_object+ The form backing object. This object must `include` Sequent::Core::Helpers::ParamSupport
|
29
|
+
# +action+ the action attribute of the <form> tag
|
30
|
+
# +method+ the method attribute of the <form> tag.
|
31
|
+
# :delete and :update will result in an extra hidden field _method to be set so sinatra will call the corresponding `delete '/' {}` or `update `/` {}` methods in your application.
|
32
|
+
# +options+ options hash
|
33
|
+
#
|
12
34
|
def html_form_for(for_object, action, method=:get, options={}, &block)
|
13
35
|
raise "Given object of class #{for_object.class} does not respond to :as_params. Are you including Sequent::Core::Helpers::ParamSupport?" if (for_object and !for_object.respond_to? :as_params)
|
14
36
|
form = Form.new(self, for_object, action, method, options.merge(role: "form"))
|
15
37
|
form.render(&block)
|
16
38
|
end
|
17
39
|
|
40
|
+
##
|
41
|
+
# Shorthand for Rack::Utils.escape_html(text)
|
42
|
+
#
|
18
43
|
def h(text)
|
19
44
|
Rack::Utils.escape_html(text)
|
20
45
|
end
|
21
46
|
|
47
|
+
##
|
48
|
+
# Shorthand for Rack::Csrf.csrf_tag(env)
|
49
|
+
#
|
22
50
|
def csrf_tag
|
23
51
|
raise "You must enable sessions to use FormHelpers" unless env
|
24
52
|
Rack::Csrf.csrf_tag(env)
|
@@ -1,7 +1,18 @@
|
|
1
1
|
module Sequent
|
2
2
|
module Web
|
3
3
|
module Sinatra
|
4
|
+
##
|
5
|
+
# Exposes various helper methods for creating form tags
|
6
|
+
#
|
4
7
|
module TagHelper
|
8
|
+
##
|
9
|
+
# creates a <input type=checkbox>
|
10
|
+
#
|
11
|
+
# Parameters
|
12
|
+
# +field+ the name of the attribute within the current object.
|
13
|
+
# +options+ Hash with optional attributes.
|
14
|
+
# :value - the default checked value if the current object has none
|
15
|
+
# :class - the css class
|
5
16
|
def raw_checkbox(field, options={})
|
6
17
|
id = css_id(@path, field)
|
7
18
|
value = param_or_default(field, options[:value]) || id
|
@@ -13,14 +24,39 @@ module Sequent
|
|
13
24
|
)
|
14
25
|
end
|
15
26
|
|
27
|
+
##
|
28
|
+
# Creates a <input type=text>
|
29
|
+
#
|
30
|
+
# Parameters
|
31
|
+
# +field+ the name of the attribute within the current object.
|
32
|
+
# +options+ Hash with optional attributes.
|
33
|
+
# :value - the default value if the current object has none
|
34
|
+
# :class - the css class
|
16
35
|
def raw_input(field, options={})
|
17
36
|
raw_field(field, "text", options)
|
18
37
|
end
|
19
38
|
|
39
|
+
##
|
40
|
+
# Creates a <input type=password>
|
41
|
+
#
|
42
|
+
# Parameters
|
43
|
+
# +field+ the name of the attribute within the current object.
|
44
|
+
# +options+ Hash with optional attributes.
|
45
|
+
# :value - the default value if the current object has none
|
46
|
+
# :class - the css class
|
20
47
|
def raw_password(field, options={})
|
21
48
|
raw_field(field, "password", options)
|
22
49
|
end
|
23
50
|
|
51
|
+
##
|
52
|
+
# Creates a <textarea>
|
53
|
+
#
|
54
|
+
# Parameters
|
55
|
+
# +field+ the name of the attribute within the current object.
|
56
|
+
# +options+ Hash with optional attributes.
|
57
|
+
# :value - the default value if the current object has none
|
58
|
+
# :class - the css class
|
59
|
+
# :rows - the number of rows of the textarea
|
24
60
|
def raw_textarea(field, options={})
|
25
61
|
value = param_or_default(field, options[:value])
|
26
62
|
|
@@ -30,10 +66,27 @@ module Sequent
|
|
30
66
|
))
|
31
67
|
end
|
32
68
|
|
69
|
+
##
|
70
|
+
# Creates a <input type=hidden>
|
71
|
+
#
|
72
|
+
# Parameters
|
73
|
+
# +field+ the name of the attribute within the current object.
|
74
|
+
# +options+ Hash with optional attributes.
|
75
|
+
# :value - the default value if the current object has none
|
76
|
+
# :class - the css class
|
33
77
|
def raw_hidden(field, options={})
|
34
78
|
raw_field(field, "hidden", options)
|
35
79
|
end
|
36
80
|
|
81
|
+
##
|
82
|
+
# Creates a <select> with <option>
|
83
|
+
#
|
84
|
+
# Parameters
|
85
|
+
# +field+ the name of the attribute within the current object.
|
86
|
+
# +values+ an array of pairs (arrays) of [value, text_to_display]
|
87
|
+
# +options+ Hash with optional attributes.
|
88
|
+
# :value - the default value if the current object has none
|
89
|
+
# :class - the css class
|
37
90
|
def raw_select(field, values, options={})
|
38
91
|
value = param_or_default(field, options[:value])
|
39
92
|
content = ""
|
@@ -49,7 +102,7 @@ module Sequent
|
|
49
102
|
|
50
103
|
def calculate_name(field)
|
51
104
|
reverse_names = tree_in_names(field)
|
52
|
-
"#{reverse_names.first}#{reverse_names[1..-1].map { |n| "[#{n}]" }.join}"
|
105
|
+
"#{reverse_names.first}#{reverse_names[1..-1].map { |n| n == '[]' ? n : "[#{n}]" }.join}"
|
53
106
|
end
|
54
107
|
|
55
108
|
def full_path(field)
|
@@ -60,9 +113,10 @@ module Sequent
|
|
60
113
|
|
61
114
|
def tree_in_names(field)
|
62
115
|
if respond_to? :path
|
63
|
-
names = [field, path]
|
116
|
+
names = [field, postfix, path].compact
|
64
117
|
parent = @parent
|
65
118
|
while parent.is_a? Fieldset
|
119
|
+
names << parent.postfix if parent.postfix
|
66
120
|
names << parent.path
|
67
121
|
parent = parent.parent
|
68
122
|
end
|
@@ -76,7 +130,6 @@ module Sequent
|
|
76
130
|
@values.nil? ? default : @values.has_key?(field.to_s) ? @values[field.to_s] || default : default
|
77
131
|
end
|
78
132
|
|
79
|
-
|
80
133
|
def id_and_text_from_value(val)
|
81
134
|
if val.is_a? Array
|
82
135
|
[val[0], val[1]]
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequent-sinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lars Vonk
|
@@ -54,6 +54,20 @@ dependencies:
|
|
54
54
|
- - "~>"
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: '2.5'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rack-test
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0.6'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0.6'
|
57
71
|
- !ruby/object:Gem::Dependency
|
58
72
|
name: rspec
|
59
73
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,6 +152,20 @@ dependencies:
|
|
138
152
|
- - "~>"
|
139
153
|
- !ruby/object:Gem::Version
|
140
154
|
version: '10.4'
|
155
|
+
- !ruby/object:Gem::Dependency
|
156
|
+
name: pry
|
157
|
+
requirement: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - "~>"
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0.10'
|
162
|
+
type: :development
|
163
|
+
prerelease: false
|
164
|
+
version_requirements: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - "~>"
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0.10'
|
141
169
|
description: Sequent is an event sourcing framework for Ruby. This gem allows Sequent
|
142
170
|
to work with Sinatra-backed applications.
|
143
171
|
email:
|
@@ -177,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
205
|
version: '0'
|
178
206
|
requirements: []
|
179
207
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.
|
208
|
+
rubygems_version: 2.4.5
|
181
209
|
signing_key:
|
182
210
|
specification_version: 4
|
183
211
|
summary: Event sourcing framework for Ruby - Sinatra adapter
|