formular 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +29 -0
  4. data/CHANGELOG.md +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +105 -0
  8. data/Rakefile +12 -0
  9. data/formular.gemspec +33 -0
  10. data/lib/formular.rb +8 -0
  11. data/lib/formular/attributes.rb +45 -0
  12. data/lib/formular/builder.rb +52 -0
  13. data/lib/formular/builders/basic.rb +64 -0
  14. data/lib/formular/builders/bootstrap3.rb +28 -0
  15. data/lib/formular/builders/bootstrap3_horizontal.rb +32 -0
  16. data/lib/formular/builders/bootstrap3_inline.rb +10 -0
  17. data/lib/formular/builders/bootstrap4.rb +36 -0
  18. data/lib/formular/builders/bootstrap4_horizontal.rb +39 -0
  19. data/lib/formular/builders/bootstrap4_inline.rb +15 -0
  20. data/lib/formular/builders/foundation6.rb +28 -0
  21. data/lib/formular/element.rb +135 -0
  22. data/lib/formular/element/bootstrap3.rb +70 -0
  23. data/lib/formular/element/bootstrap3/checkable_control.rb +105 -0
  24. data/lib/formular/element/bootstrap3/column_control.rb +45 -0
  25. data/lib/formular/element/bootstrap3/horizontal.rb +146 -0
  26. data/lib/formular/element/bootstrap3/input_group.rb +83 -0
  27. data/lib/formular/element/bootstrap4.rb +49 -0
  28. data/lib/formular/element/bootstrap4/checkable_control.rb +94 -0
  29. data/lib/formular/element/bootstrap4/custom_control.rb +120 -0
  30. data/lib/formular/element/bootstrap4/horizontal.rb +87 -0
  31. data/lib/formular/element/foundation6.rb +69 -0
  32. data/lib/formular/element/foundation6/checkable_control.rb +72 -0
  33. data/lib/formular/element/foundation6/input_group.rb +88 -0
  34. data/lib/formular/element/foundation6/wrapped_control.rb +21 -0
  35. data/lib/formular/element/module.rb +35 -0
  36. data/lib/formular/element/modules/checkable.rb +96 -0
  37. data/lib/formular/element/modules/collection.rb +17 -0
  38. data/lib/formular/element/modules/container.rb +60 -0
  39. data/lib/formular/element/modules/control.rb +42 -0
  40. data/lib/formular/element/modules/error.rb +48 -0
  41. data/lib/formular/element/modules/hint.rb +36 -0
  42. data/lib/formular/element/modules/label.rb +30 -0
  43. data/lib/formular/element/modules/wrapped_control.rb +73 -0
  44. data/lib/formular/elements.rb +295 -0
  45. data/lib/formular/helper.rb +53 -0
  46. data/lib/formular/html_block.rb +70 -0
  47. data/lib/formular/path.rb +30 -0
  48. data/lib/formular/version.rb +3 -0
  49. metadata +247 -0
@@ -0,0 +1,53 @@
1
+ module Formular
2
+ # TODO: indirectly tested in erb_test and slim_test. Should probably test directly
3
+ module Helper
4
+ def form(model, url, **options, &block)
5
+ form_options = options
6
+ builder_options = form_options.select { |k, v| form_options.delete(k) || true if [:builder, :model, :path_prefix, :errors, :elements].include?(k) }
7
+
8
+ form_options[:action] ||= url
9
+ builder(model, builder_options).form(form_options, &block)
10
+ end
11
+
12
+ BUILDERS = {
13
+ basic: 'Formular::Builders::Basic',
14
+ bootstrap3: 'Formular::Builders::Bootstrap3',
15
+ bootstrap4: 'Formular::Builders::Bootstrap4',
16
+ bootstrap3_inline: 'Formular::Builders::Bootstrap3Inline',
17
+ bootstrap4_inline: 'Formular::Builders::Bootstrap4Inline',
18
+ bootstrap3_horizontal: 'Formular::Builders::Bootstrap3Horizontal',
19
+ bootstrap4_horizontal: 'Formular::Builders::Bootstrap4Horizontal',
20
+ foundation6: 'Formular::Builders::Foundation6'
21
+ }.freeze
22
+
23
+ class << self
24
+ attr_writer :builder
25
+
26
+ def builder(name = nil)
27
+ name ||= :basic
28
+ require "formular/builders/#{name}"
29
+ self.builder = Formular::Builders.const_get(BUILDERS.fetch(name)) # Formular::Builders::Bootstrap3
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def builder(model, **options)
36
+ builder = Formular::Helper.builder(options.delete(:builder))
37
+ options[:model] ||= model
38
+
39
+ builder.new(options)
40
+ end
41
+ end # module Helper
42
+
43
+ module RailsHelper
44
+ include Helper
45
+
46
+ def trb_form_for(model, url, **options, &block)
47
+ options[:csrf_token] ||= form_authenticity_token
48
+ options[:csrf_token_name] ||= request_forgery_protection_token.to_s
49
+
50
+ form(model, url, options, &block)
51
+ end
52
+ end # module RailsHelper
53
+ end # module Formular
@@ -0,0 +1,70 @@
1
+ module Formular
2
+ # the HtmlBlock class is responsible for converting an element into an html string using
3
+ # the provided function providing the element as a variable to that function.
4
+ # this class also provides some simple helpers to make it easier to define your html.
5
+ class HtmlBlock
6
+ def initialize(element, fn)
7
+ @fn = fn
8
+ @element = element
9
+ @context = get_context
10
+ end
11
+ attr_reader :fn, :element
12
+
13
+ # this calls our html function passing the element instance as a variable.
14
+ # It returns our html as a string
15
+ def call
16
+ @output = ''
17
+ instance_exec(element, &fn).to_s
18
+ end
19
+
20
+ # append a string to the output buffer.
21
+ # Useful when your html block is a bit more than one line
22
+ def concat(content)
23
+ @output << content.to_s
24
+ end
25
+
26
+ # return the start/opening tag with the element
27
+ # attributes hash converted into valid html attributes
28
+ def start_tag
29
+ if element.attributes.empty?
30
+ "<#{element.tag}>"
31
+ else
32
+ "<#{element.tag} #{element.attributes.to_html}>"
33
+ end
34
+ end
35
+
36
+ # return a closed start tag (e.g. <input name="body"/>)
37
+ def closed_start_tag
38
+ start_tag.gsub('>', '/>')
39
+ end
40
+
41
+ # returns the end/ closing tag for an element
42
+ def end_tag
43
+ "</#{element.tag}>"
44
+ end
45
+
46
+ private
47
+
48
+ # I will confess that these two come straight from Hanami
49
+ # https://github.com/hanami/helpers/blob/master/lib/hanami/helpers/html_helper/html_builder.rb
50
+
51
+ if RUBY_VERSION >= '2.2'# && !Utils.jruby?
52
+ def get_context
53
+ fn.binding.receiver
54
+ end
55
+ else
56
+ def get_context
57
+ eval 'self', fn.binding
58
+ end
59
+ end
60
+
61
+ # Forward missing methods to the current context.
62
+ # This allows to access views local variables from nested content blocks.
63
+ #
64
+ # @since 0.1.0
65
+ # @api private
66
+ def method_missing(m, *args, &blk)
67
+ @context.__send__(m, *args, &blk)
68
+ end
69
+ end # class HtmlBlock
70
+ end # module Formular
@@ -0,0 +1,30 @@
1
+ module Formular
2
+ class Path < Array
3
+ # name #attribute without model...
4
+ # [User, :name] => user[name] #regular attribute
5
+ # [User, roles: 0, :name] => user[roles][][name]
6
+
7
+ # DISCUSS: Should we also enable the following for rails accepts_nested_attributes support
8
+ # [User, roles: 0, :name => user[roles_attributes][0][name]
9
+ def to_encoded_name
10
+ map.with_index do |segment, i|
11
+ first_or_last = i == 0 || i == size
12
+
13
+ if first_or_last
14
+ segment.is_a?(Array) ? "#{segment.first}[]" : segment
15
+ else
16
+ segment.is_a?(Array) ? "[#{segment.first}][]" : "[#{segment}]"
17
+ end
18
+ end.join('')
19
+ end
20
+
21
+ # need to inject the index in here... else we will end up with the same ids
22
+ # [User, :name] => user_name #regular attribute
23
+ # [User, roles: 0, :name] => user_roles_0_name #nested not rails
24
+ # DISCUSS: Should we also enable the following for rails accepts_nested_attributes support
25
+ # [User, roles: 0, :name => user_roles_attributes_0_name #nested rails
26
+ def to_encoded_id
27
+ map { |segment| segment.is_a?(Array) ? segment.join('_') : segment }.join('_')
28
+ end
29
+ end # class Attributes
30
+ end # module Formular
@@ -0,0 +1,3 @@
1
+ module Formular
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,247 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formular
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Sutterer
8
+ - Fran Worley
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-09-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: declarative
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.0.4
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.0.4
28
+ - !ruby/object:Gem::Dependency
29
+ name: uber
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.11
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.0.11
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: reform
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: dry-validation
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: trailblazer-cells
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: cells-slim
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: cells-erb
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: minitest
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ - !ruby/object:Gem::Dependency
155
+ name: minitest-line
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ description: Customizable, fast form builder based on Cells. Framework-agnostic.
169
+ email:
170
+ - apotonick@gmail.com
171
+ - frances@safetytoolbox.co.uk
172
+ executables: []
173
+ extensions: []
174
+ extra_rdoc_files: []
175
+ files:
176
+ - ".gitignore"
177
+ - ".travis.yml"
178
+ - CHANGELOG.md
179
+ - Gemfile
180
+ - LICENSE.txt
181
+ - README.md
182
+ - Rakefile
183
+ - formular.gemspec
184
+ - lib/formular.rb
185
+ - lib/formular/attributes.rb
186
+ - lib/formular/builder.rb
187
+ - lib/formular/builders/basic.rb
188
+ - lib/formular/builders/bootstrap3.rb
189
+ - lib/formular/builders/bootstrap3_horizontal.rb
190
+ - lib/formular/builders/bootstrap3_inline.rb
191
+ - lib/formular/builders/bootstrap4.rb
192
+ - lib/formular/builders/bootstrap4_horizontal.rb
193
+ - lib/formular/builders/bootstrap4_inline.rb
194
+ - lib/formular/builders/foundation6.rb
195
+ - lib/formular/element.rb
196
+ - lib/formular/element/bootstrap3.rb
197
+ - lib/formular/element/bootstrap3/checkable_control.rb
198
+ - lib/formular/element/bootstrap3/column_control.rb
199
+ - lib/formular/element/bootstrap3/horizontal.rb
200
+ - lib/formular/element/bootstrap3/input_group.rb
201
+ - lib/formular/element/bootstrap4.rb
202
+ - lib/formular/element/bootstrap4/checkable_control.rb
203
+ - lib/formular/element/bootstrap4/custom_control.rb
204
+ - lib/formular/element/bootstrap4/horizontal.rb
205
+ - lib/formular/element/foundation6.rb
206
+ - lib/formular/element/foundation6/checkable_control.rb
207
+ - lib/formular/element/foundation6/input_group.rb
208
+ - lib/formular/element/foundation6/wrapped_control.rb
209
+ - lib/formular/element/module.rb
210
+ - lib/formular/element/modules/checkable.rb
211
+ - lib/formular/element/modules/collection.rb
212
+ - lib/formular/element/modules/container.rb
213
+ - lib/formular/element/modules/control.rb
214
+ - lib/formular/element/modules/error.rb
215
+ - lib/formular/element/modules/hint.rb
216
+ - lib/formular/element/modules/label.rb
217
+ - lib/formular/element/modules/wrapped_control.rb
218
+ - lib/formular/elements.rb
219
+ - lib/formular/helper.rb
220
+ - lib/formular/html_block.rb
221
+ - lib/formular/path.rb
222
+ - lib/formular/version.rb
223
+ homepage: http://trailblazer.to/gems/formular.html
224
+ licenses:
225
+ - MIT
226
+ metadata: {}
227
+ post_install_message:
228
+ rdoc_options: []
229
+ require_paths:
230
+ - lib
231
+ required_ruby_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ required_rubygems_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 2.5.1
244
+ signing_key:
245
+ specification_version: 4
246
+ summary: Form builder based on Cells. Fast, Furious, and Framework-Agnostic.
247
+ test_files: []