lotus-helpers 0.2.0 → 0.2.5

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: c48523257c329e031af02471ad83b63a56290bca
4
- data.tar.gz: ec1dba8d370b280d0fb14992b2955c916e9c96da
3
+ metadata.gz: 30f2cf703b4746c9bd13c43eb4f7e8d080251dd2
4
+ data.tar.gz: 78149500f0aefbcd5748f5184d5927cbc3e524e1
5
5
  SHA512:
6
- metadata.gz: ee14aad1c7167c3dd942f22b59a15776c3ed6381d8eef769322b4f377716e62fe07fbe0b2666f7d1e2ce0ab0ad637560e6dbcc5649dd3515cdad60b80d2dacd0
7
- data.tar.gz: 9bf9dcaf0da1206f54d11188dc36d0ebc876d76d80c9c1a1498f177560096c66d79c1725255ee1311a349bc183390a4f209270c303be554c97743edca81b738a
6
+ metadata.gz: 903e02d8560775ebae4c3801cd263d7477766065bf31936e845c245c4a1d526093b2375e7900e5244522b2e9aaafc7c4f84c47cb9f8c1c4fa38b15bb2019a5dc
7
+ data.tar.gz: 3e840682aef29d505b238de7085cb6703ae31dff639c8b31b04064f4498a5d87f553fe09ae302cd2419d45bfaa8fffa15d7016425f3c24291e3f83b5e9310c34
data/README.md CHANGED
@@ -22,7 +22,7 @@ View helpers for Ruby applications
22
22
 
23
23
  ## Rubies
24
24
 
25
- __Lotus::Helpers__ supports Ruby (MRI) 2+ and JRuby 1.7 (with 2.0 mode).
25
+ __Lotus::Helpers__ supports Ruby (MRI) 2+
26
26
 
27
27
  ## Installation
28
28
 
@@ -0,0 +1 @@
1
+ require 'lotus/helpers'
@@ -38,13 +38,14 @@ module Lotus
38
38
  # * <tt>fields_for</tt>
39
39
  # * <tt>form_for</tt>
40
40
  # * <tt>label</tt>
41
+ # * <tt>text_area</tt>
41
42
  # * <tt>text_field</tt>
42
43
  # * <tt>password_field</tt>
43
44
  # * <tt>radio_button</tt>
44
45
  # * <tt>select</tt>
45
46
  # * <tt>submit</tt>
46
47
  #
47
- # @since x.x.x
48
+ # @since 0.2.0
48
49
  #
49
50
  # @see Lotus::Helpers::FormHelper#form_for
50
51
  # @see Lotus::Helpers::HtmlHelper
@@ -77,13 +78,13 @@ module Lotus
77
78
  module FormHelper
78
79
  # Default HTTP method for form
79
80
  #
80
- # @since x.x.x
81
+ # @since 0.2.0
81
82
  # @api private
82
83
  DEFAULT_METHOD = 'POST'.freeze
83
84
 
84
85
  # Default charset
85
86
  #
86
- # @since x.x.x
87
+ # @since 0.2.0
87
88
  # @api private
88
89
  DEFAULT_CHARSET = 'utf-8'.freeze
89
90
 
@@ -91,29 +92,29 @@ module Lotus
91
92
  #
92
93
  # This key is shared with <tt>lotusrb</tt>, <tt>lotus-controller</tt>.
93
94
  #
94
- # @since x.x.x
95
+ # @since 0.2.0
95
96
  # @api private
96
97
  CSRF_TOKEN = :_csrf_token
97
98
 
98
99
  # Form object
99
100
  #
100
- # @since x.x.x
101
+ # @since 0.2.0
101
102
  class Form
102
103
  # @return [Symbol] the form name
103
104
  #
104
- # @since x.x.x
105
+ # @since 0.2.0
105
106
  # @api private
106
107
  attr_reader :name
107
108
 
108
109
  # @return [String] the form action
109
110
  #
110
- # @since x.x.x
111
+ # @since 0.2.0
111
112
  # @api private
112
113
  attr_reader :url
113
114
 
114
115
  # @return [::Hash] the form values
115
116
  #
116
- # @since x.x.x
117
+ # @since 0.2.0
117
118
  # @api private
118
119
  attr_reader :values
119
120
 
@@ -142,7 +143,7 @@ module Lotus
142
143
  # @param attributes [Hash,NilClass] a Hash of attributes to pass to the
143
144
  # <tt>form</tt> tag
144
145
  #
145
- # @since x.x.x
146
+ # @since 0.2.0
146
147
  #
147
148
  # @example Pass A Value
148
149
  # # Given the following view
@@ -206,7 +207,7 @@ module Lotus
206
207
  #
207
208
  # @return [String] the method for the action
208
209
  #
209
- # @since x.x.x
210
+ # @since 0.2.0
210
211
  # @api private
211
212
  #
212
213
  # @see Lotus::Helpers::FormHelper::DEFAULT_METHOD
@@ -234,7 +235,7 @@ module Lotus
234
235
  #
235
236
  # @return [Lotus::Helpers::FormHelper::FormBuilder] the form builder
236
237
  #
237
- # @since x.x.x
238
+ # @since 0.2.0
238
239
  #
239
240
  # @see Lotus::Helpers::FormHelper
240
241
  # @see Lotus::Helpers::FormHelper::Form
@@ -410,7 +411,7 @@ module Lotus
410
411
  #
411
412
  # @return [String,NilClass] token, if present
412
413
  #
413
- # @since x.x.x
414
+ # @since 0.2.0
414
415
  def csrf_token
415
416
  if defined?(session)
416
417
  session[CSRF_TOKEN]
@@ -8,19 +8,19 @@ module Lotus
8
8
  module FormHelper
9
9
  # Form builder
10
10
  #
11
- # @since x.x.x
11
+ # @since 0.2.0
12
12
  #
13
13
  # @see Lotus::Helpers::HtmlHelper::HtmlBuilder
14
14
  class FormBuilder < ::Lotus::Helpers::HtmlHelper::HtmlBuilder
15
15
  # Set of HTTP methods that are understood by web browsers
16
16
  #
17
- # @since x.x.x
17
+ # @since 0.2.0
18
18
  # @api private
19
19
  BROWSER_METHODS = ['GET', 'POST'].freeze
20
20
 
21
21
  # Checked attribute value
22
22
  #
23
- # @since x.x.x
23
+ # @since 0.2.0
24
24
  # @api private
25
25
  #
26
26
  # @see Lotus::Helpers::FormHelper::FormBuilder#radio_button
@@ -28,7 +28,7 @@ module Lotus
28
28
 
29
29
  # Selected attribute value for option
30
30
  #
31
- # @since x.x.x
31
+ # @since 0.2.0
32
32
  # @api private
33
33
  #
34
34
  # @see Lotus::Helpers::FormHelper::FormBuilder#select
@@ -36,7 +36,7 @@ module Lotus
36
36
 
37
37
  # Separator for accept attribute of file input
38
38
  #
39
- # @since x.x.x
39
+ # @since 0.2.0
40
40
  # @api private
41
41
  #
42
42
  # @see Lotus::Helpers::FormHelper::FormBuilder#file_input
@@ -44,7 +44,7 @@ module Lotus
44
44
 
45
45
  # Replacement for input id interpolation
46
46
  #
47
- # @since x.x.x
47
+ # @since 0.2.0
48
48
  # @api private
49
49
  #
50
50
  # @see Lotus::Helpers::FormHelper::FormBuilder#_input_id
@@ -52,7 +52,7 @@ module Lotus
52
52
 
53
53
  # Replacement for input value interpolation
54
54
  #
55
- # @since x.x.x
55
+ # @since 0.2.0
56
56
  # @api private
57
57
  #
58
58
  # @see Lotus::Helpers::FormHelper::FormBuilder#_value
@@ -60,7 +60,7 @@ module Lotus
60
60
 
61
61
  # Default value for unchecked check box
62
62
  #
63
- # @since x.x.x
63
+ # @since 0.2.0
64
64
  # @api private
65
65
  #
66
66
  # @see Lotus::Helpers::FormHelper::FormBuilder#check_box
@@ -68,7 +68,7 @@ module Lotus
68
68
 
69
69
  # Default value for checked check box
70
70
  #
71
- # @since x.x.x
71
+ # @since 0.2.0
72
72
  # @api private
73
73
  #
74
74
  # @see Lotus::Helpers::FormHelper::FormBuilder#check_box
@@ -96,7 +96,7 @@ module Lotus
96
96
  #
97
97
  # @return [Lotus::Helpers::FormHelper::FormBuilder] the form builder
98
98
  #
99
- # @since x.x.x
99
+ # @since 0.2.0
100
100
  # @api private
101
101
  def initialize(form, attributes, context = nil, &blk)
102
102
  super()
@@ -122,7 +122,7 @@ module Lotus
122
122
  #
123
123
  # @return [Lotus::Utils::Escape::SafeString] the output
124
124
  #
125
- # @since x.x.x
125
+ # @since 0.2.0
126
126
  # @api private
127
127
  #
128
128
  # @see Lotus::Helpers::HtmlHelper::HtmlBuilder#to_s
@@ -144,7 +144,7 @@ module Lotus
144
144
  # @param name [Symbol] the nested name, it's used to generate input
145
145
  # names, ids, and to lookup params to fill values.
146
146
  #
147
- # @since x.x.x
147
+ # @since 0.2.0
148
148
  #
149
149
  # @example Basic usage
150
150
  # <%=
@@ -211,7 +211,7 @@ module Lotus
211
211
  # @param content [Symbol,String] the field name or a content string
212
212
  # @param attributes [Hash] HTML attributes to pass to the label tag
213
213
  #
214
- # @since x.x.x
214
+ # @since 0.2.0
215
215
  #
216
216
  # @example Basic usage
217
217
  # <%=
@@ -220,7 +220,7 @@ module Lotus
220
220
  # %>
221
221
  #
222
222
  # # Output:
223
- # # <label for="book-extended-title">Extended Title</label>
223
+ # # <label for="book-extended-title">Extended title</label>
224
224
  #
225
225
  # @example Custom content
226
226
  # <%=
@@ -231,6 +231,15 @@ module Lotus
231
231
  # # Output:
232
232
  # # <label for="book-extended-title">Title</label>
233
233
  #
234
+ # @example Custom "for" attribute
235
+ # <%=
236
+ # # ...
237
+ # label :extended_title, for: 'ext-title'
238
+ # %>
239
+ #
240
+ # # Output:
241
+ # # <label for="ext-title">Extended title</label>
242
+ #
234
243
  # @example Nested fields usage
235
244
  # <%=
236
245
  # # ...
@@ -245,7 +254,12 @@ module Lotus
245
254
  # # <input type="text" name="delivery[address][city] id="delivery-address-city" value="">
246
255
  def label(content, attributes = {})
247
256
  attributes = { for: _for(content, attributes.delete(:for)) }.merge(attributes)
248
- content = Utils::String.new(content).titleize
257
+ content = case content
258
+ when String, Lotus::Utils::String
259
+ content
260
+ else
261
+ Utils::String.new(content).capitalize
262
+ end
249
263
 
250
264
  super(content, attributes)
251
265
  end
@@ -273,7 +287,7 @@ module Lotus
273
287
  # @option attributes [String] :checked_value (defaults to "1")
274
288
  # @option attributes [String] :unchecked_value (defaults to "0")
275
289
  #
276
- # @since x.x.x
290
+ # @since 0.2.0
277
291
  #
278
292
  # @example Basic usage
279
293
  # <%=
@@ -349,7 +363,7 @@ module Lotus
349
363
  # @param name [Symbol] the input name
350
364
  # @param attributes [Hash] HTML attributes to pass to the input tag
351
365
  #
352
- # @since x.x.x
366
+ # @since 0.2.0
353
367
  #
354
368
  # @example Basic usage
355
369
  # <%=
@@ -368,7 +382,7 @@ module Lotus
368
382
  # @param name [Symbol] the input name
369
383
  # @param attributes [Hash] HTML attributes to pass to the input tag
370
384
  #
371
- # @since x.x.x
385
+ # @since 0.2.0
372
386
  #
373
387
  # @example Basic usage
374
388
  # <%=
@@ -387,7 +401,7 @@ module Lotus
387
401
  # @param name [Symbol] the input name
388
402
  # @param attributes [Hash] HTML attributes to pass to the input tag
389
403
  #
390
- # @since x.x.x
404
+ # @since 0.2.0
391
405
  #
392
406
  # @example Basic usage
393
407
  # <%=
@@ -406,7 +420,7 @@ module Lotus
406
420
  # @param name [Symbol] the input name
407
421
  # @param attributes [Hash] HTML attributes to pass to the input tag
408
422
  #
409
- # @since x.x.x
423
+ # @since 0.2.0
410
424
  #
411
425
  # @example Basic usage
412
426
  # <%=
@@ -425,7 +439,7 @@ module Lotus
425
439
  # @param name [Symbol] the input name
426
440
  # @param attributes [Hash] HTML attributes to pass to the input tag
427
441
  #
428
- # @since x.x.x
442
+ # @since 0.2.0
429
443
  #
430
444
  # @example Basic usage
431
445
  # <%=
@@ -444,7 +458,7 @@ module Lotus
444
458
  # @param name [Symbol] the input name
445
459
  # @param attributes [Hash] HTML attributes to pass to the input tag
446
460
  #
447
- # @since x.x.x
461
+ # @since 0.2.0
448
462
  #
449
463
  # @example Basic usage
450
464
  # <%=
@@ -466,7 +480,7 @@ module Lotus
466
480
  # @param attributes [Hash] HTML attributes to pass to the input tag
467
481
  # @option attributes [String,Array] :accept Optional set of accepted MIME Types
468
482
  #
469
- # @since x.x.x
483
+ # @since 0.2.0
470
484
  #
471
485
  # @example Basic usage
472
486
  # <%=
@@ -501,12 +515,103 @@ module Lotus
501
515
  input(attributes)
502
516
  end
503
517
 
518
+ # Number input
519
+ #
520
+ # @param name [Symbol] the input name
521
+ # @param attributes [Hash] HTML attributes to pass to the number input
522
+ #
523
+ # @example Basic usage
524
+ # <%=
525
+ # # ...
526
+ # number_field :percent_read
527
+ # %>
528
+ #
529
+ # # Output:
530
+ # # <input type="number" name="book[percent_read]" id="book-percent-read" value="">
531
+ #
532
+ # You can also make use of the 'max', 'min', and 'step' attributes for
533
+ # the HTML5 number field.
534
+ #
535
+ # @example Advanced attributes
536
+ # <%=
537
+ # # ...
538
+ # number_field :priority, min: 1, max: 10, step: 1
539
+ # %>
540
+ #
541
+ # # Output:
542
+ # # <input type="number" name="book[percent_read]" id="book-precent-read" value="" min="1" max="10" step="1">
543
+ def number_field(name, attributes = {})
544
+ input _attributes(:number, name, attributes)
545
+ end
546
+
547
+ # Text-area input
548
+ #
549
+ # @param name [Symbol] the input name
550
+ # @param content [String] the content of the textarea
551
+ # @param attributes [Hash] HTML attributes to pass to the textarea tag
552
+ #
553
+ # @since 0.2.5
554
+ #
555
+ # @example Basic usage
556
+ # <%=
557
+ # # ...
558
+ # text_area :hobby
559
+ # %>
560
+ #
561
+ # # Output:
562
+ # # <textarea name="user[hobby]" id="user-hobby"></textarea>
563
+ #
564
+ # @example Set content
565
+ # <%=
566
+ # # ...
567
+ # text_area :hobby, 'Football'
568
+ # %>
569
+ #
570
+ # # Output:
571
+ # # <textarea name="user[hobby]" id="user-hobby">Football</textarea>
572
+ #
573
+ # @example Set content and HTML attributes
574
+ # <%=
575
+ # # ...
576
+ # text_area :hobby, 'Football', class: 'form-control'
577
+ # %>
578
+ #
579
+ # # Output:
580
+ # # <textarea name="user[hobby]" id="user-hobby" class="form-control">Football</textarea>
581
+ #
582
+ # @example Omit content and specify HTML attributes
583
+ # <%=
584
+ # # ...
585
+ # text_area :hobby, class: 'form-control'
586
+ # %>
587
+ #
588
+ # # Output:
589
+ # # <textarea name="user[hobby]" id="user-hobby" class="form-control"></textarea>
590
+ #
591
+ # @example Force blank value
592
+ # <%=
593
+ # # ...
594
+ # text_area :hobby, '', class: 'form-control'
595
+ # %>
596
+ #
597
+ # # Output:
598
+ # # <textarea name="user[hobby]" id="user-hobby" class="form-control"></textarea>
599
+ def text_area(name, content = nil, attributes = {})
600
+ if content.respond_to?(:to_hash)
601
+ attributes = content
602
+ content = nil
603
+ end
604
+
605
+ attributes = {name: _input_name(name), id: _input_id(name)}.merge(attributes)
606
+ textarea(content || _value(name), attributes)
607
+ end
608
+
504
609
  # Text input
505
610
  #
506
611
  # @param name [Symbol] the input name
507
612
  # @param attributes [Hash] HTML attributes to pass to the input tag
508
613
  #
509
- # @since x.x.x
614
+ # @since 0.2.0
510
615
  #
511
616
  # @example Basic usage
512
617
  # <%=
@@ -531,7 +636,7 @@ module Lotus
531
636
  # @param value [String] the input value
532
637
  # @param attributes [Hash] HTML attributes to pass to the input tag
533
638
  #
534
- # @since x.x.x
639
+ # @since 0.2.0
535
640
  #
536
641
  # @example Basic usage
537
642
  # <%=
@@ -571,7 +676,7 @@ module Lotus
571
676
  # @param name [Symbol] the input name
572
677
  # @param attributes [Hash] HTML attributes to pass to the input tag
573
678
  #
574
- # @since x.x.x
679
+ # @since 0.2.0
575
680
  #
576
681
  # @example Basic usage
577
682
  # <%=
@@ -596,7 +701,7 @@ module Lotus
596
701
  # it automatically sets the <tt>selected</tt> attribute on the <tt><option></tt> tag.
597
702
  # This Lotus::Controller integration happens without any developer intervention.
598
703
  #
599
- # @since x.x.x
704
+ # @since 0.2.0
600
705
  #
601
706
  # @example Basic usage
602
707
  # <%=
@@ -649,7 +754,7 @@ module Lotus
649
754
  # @param content [String] The content
650
755
  # @param attributes [Hash] HTML attributes to pass to the button tag
651
756
  #
652
- # @since x.x.x
757
+ # @since 0.2.0
653
758
  #
654
759
  # @example Basic usage
655
760
  # <%=
@@ -668,7 +773,7 @@ module Lotus
668
773
  # A set of options to pass to the sub form helpers.
669
774
  #
670
775
  # @api private
671
- # @since x.x.x
776
+ # @since 0.2.0
672
777
  def options
673
778
  Hash[name: @name, values: @values, verb: @verb, csrf_token: @csrf_token]
674
779
  end
@@ -677,7 +782,7 @@ module Lotus
677
782
  # Check the current builder is top-level
678
783
  #
679
784
  # @api private
680
- # @since x.x.x
785
+ # @since 0.2.0
681
786
  def toplevel?
682
787
  @attributes.any?
683
788
  end
@@ -685,7 +790,7 @@ module Lotus
685
790
  # Prepare for method override
686
791
  #
687
792
  # @api private
688
- # @since x.x.x
793
+ # @since 0.2.0
689
794
  def _method_override!
690
795
  verb = (@attributes.fetch(:method) { DEFAULT_METHOD }).to_s.upcase
691
796
 
@@ -700,7 +805,7 @@ module Lotus
700
805
  # Return CSRF Protection token from view context
701
806
  #
702
807
  # @api private
703
- # @since x.x.x
808
+ # @since 0.2.0
704
809
  def csrf_token
705
810
  @context.csrf_token if @context.respond_to?(:csrf_token)
706
811
  end
@@ -708,7 +813,7 @@ module Lotus
708
813
  # Return a set of default HTML attributes
709
814
  #
710
815
  # @api private
711
- # @since x.x.x
816
+ # @since 0.2.0
712
817
  def _attributes(type, name, attributes)
713
818
  { type: type, name: _input_name(name), id: _input_id(name), value: _value(name) }.merge(attributes)
714
819
  end
@@ -716,7 +821,7 @@ module Lotus
716
821
  # Input <tt>name</tt> HTML attribute
717
822
  #
718
823
  # @api private
719
- # @since x.x.x
824
+ # @since 0.2.0
720
825
  def _input_name(name)
721
826
  "#{ @name }[#{ name }]"
722
827
  end
@@ -724,7 +829,7 @@ module Lotus
724
829
  # Input <tt>id</tt> HTML attribute
725
830
  #
726
831
  # @api private
727
- # @since x.x.x
832
+ # @since 0.2.0
728
833
  def _input_id(name)
729
834
  name = _input_name(name).gsub(/\[(?<token>[[[:word:]]\-]*)\]/, INPUT_ID_REPLACEMENT)
730
835
  Utils::String.new(name).dasherize
@@ -733,7 +838,7 @@ module Lotus
733
838
  # Input <tt>value</tt> HTML attribute
734
839
  #
735
840
  # @api private
736
- # @since x.x.x
841
+ # @since 0.2.0
737
842
  def _value(name)
738
843
  name = _input_name(name).gsub(/\[(?<token>[[:word:]]*)\]/, INPUT_VALUE_REPLACEMENT)
739
844
  @values.get(name)
@@ -742,15 +847,20 @@ module Lotus
742
847
  # Input <tt>for</tt> HTML attribute
743
848
  #
744
849
  # @api private
745
- # @since x.x.x
850
+ # @since 0.2.0
746
851
  def _for(content, name)
747
- _input_id(name || content)
852
+ case name
853
+ when String, Lotus::Utils::String
854
+ name
855
+ else
856
+ _input_id(name || content)
857
+ end
748
858
  end
749
859
 
750
860
  # Hidden field for check box
751
861
  #
752
862
  # @api private
753
- # @since x.x.x
863
+ # @since 0.2.0
754
864
  #
755
865
  # @see Lotus::Helpers::FormHelper::FormBuilder#check_box
756
866
  def _hidden_field_for_check_box(name, attributes)
@@ -766,7 +876,7 @@ module Lotus
766
876
  # HTML attributes for check box
767
877
  #
768
878
  # @api private
769
- # @since x.x.x
879
+ # @since 0.2.0
770
880
  #
771
881
  # @see Lotus::Helpers::FormHelper::FormBuilder#check_box
772
882
  def _attributes_for_check_box(name, attributes)
@@ -787,4 +897,3 @@ module Lotus
787
897
  end
788
898
  end
789
899
  end
790
-
@@ -5,7 +5,7 @@ module Lotus
5
5
  module FormHelper
6
6
  # HTML form node
7
7
  #
8
- # @since x.x.x
8
+ # @since 0.2.0
9
9
  # @api private
10
10
  #
11
11
  # @see Lotus::Helpers::HtmlHelper::HtmlNode
@@ -19,7 +19,7 @@ module Lotus
19
19
  #
20
20
  # @return [Lotus::Helpers::FormHelper::HtmlNode]
21
21
  #
22
- # @since x.x.x
22
+ # @since 0.2.0
23
23
  # @api private
24
24
  def initialize(name, content, attributes, options)
25
25
  super
@@ -38,7 +38,7 @@ module Lotus
38
38
  #
39
39
  # @return [String] the content
40
40
  #
41
- # @since x.x.x
41
+ # @since 0.2.0
42
42
  # @api private
43
43
  #
44
44
  # @see Lotus::Helpers::HtmlHelper::HtmlNode#content
@@ -50,7 +50,7 @@ module Lotus
50
50
 
51
51
  # Inject a hidden field to make Method Override possible
52
52
  #
53
- # @since x.x.x
53
+ # @since 0.2.0
54
54
  # @api private
55
55
  def _method_override!
56
56
  return if @verb.nil?
@@ -63,7 +63,7 @@ module Lotus
63
63
 
64
64
  # Inject a hidden field for CSRF Protection token
65
65
  #
66
- # @since x.x.x
66
+ # @since 0.2.0
67
67
  # @api private
68
68
  def _csrf_protection!
69
69
  return if @csrf_token.nil?
@@ -6,6 +6,22 @@ module Lotus
6
6
  # @since 0.1.0
7
7
  # @api private
8
8
  class EmptyHtmlNode
9
+ # List of attributes that get special treatment when rendering.
10
+ #
11
+ # @since 0.2.5
12
+ # @api private
13
+ #
14
+ # @see http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attribute
15
+ BOOLEAN_ATTRIBUTES = %w{allowfullscreen async autobuffer autofocus
16
+ autoplay checked compact controls declare default defaultchecked
17
+ defaultmuted defaultselected defer disabled draggable enabled
18
+ formnovalidate hidden indeterminate inert ismap itemscope loop
19
+ multiple muted nohref noresize noshade novalidate nowrap open
20
+ pauseonexit pubdate readonly required reversed scoped seamless
21
+ selected sortable spellcheck translate truespeed typemustmatch
22
+ visible
23
+ }.freeze
24
+
9
25
  # Attributes separator
10
26
  #
11
27
  # @since 0.1.0
@@ -45,13 +61,30 @@ module Lotus
45
61
  # @api private
46
62
  def attributes
47
63
  return if @attributes.nil?
48
- result = [nil]
64
+ result = ''
49
65
 
50
- @attributes.each do |name, value|
51
- result << %(#{ name }="#{ value }")
66
+ @attributes.each do |attribute_name, value|
67
+ if boolean_attribute?(attribute_name)
68
+ result << boolean_attribute(attribute_name, value) if value
69
+ else
70
+ result << attribute(attribute_name, value)
71
+ end
52
72
  end
53
73
 
54
- result.join(ATTRIBUTES_SEPARATOR)
74
+ result
75
+ end
76
+
77
+ def boolean_attribute?(attribute_name)
78
+ BOOLEAN_ATTRIBUTES.include?(attribute_name.to_s)
79
+ end
80
+
81
+ # Do not render boolean attributes when their value is _false_.
82
+ def boolean_attribute(attribute_name, value)
83
+ %(#{ATTRIBUTES_SEPARATOR}#{ attribute_name }="#{ attribute_name }")
84
+ end
85
+
86
+ def attribute(attribute_name, value)
87
+ %(#{ATTRIBUTES_SEPARATOR}#{ attribute_name }="#{ value }")
55
88
  end
56
89
  end
57
90
  end
@@ -3,6 +3,7 @@ require 'lotus/utils/class_attribute'
3
3
  require 'lotus/utils/escape'
4
4
  require 'lotus/helpers/html_helper/empty_html_node'
5
5
  require 'lotus/helpers/html_helper/html_node'
6
+ require 'lotus/helpers/html_helper/text_node'
6
7
 
7
8
  module Lotus
8
9
  module Helpers
@@ -253,6 +254,36 @@ module Lotus
253
254
  self
254
255
  end
255
256
 
257
+ # Defines a plain string of text. This particularly useful when you
258
+ # want to build more complex HTML.
259
+ #
260
+ # @param content [String] the text to be rendered.
261
+ #
262
+ # @return [self]
263
+ #
264
+ # @see Lotus::Helpers::HtmlHelper
265
+ # @see Lotus::Helpers::HtmlHelper::TextNode
266
+ #
267
+ # @example
268
+ #
269
+ # html.label do
270
+ # text "Option 1"
271
+ # radio_button :option, 1
272
+ # end
273
+ #
274
+ # # <label>
275
+ # # Option 1
276
+ # # <input type="radio" name="option" value="1" />
277
+ # # </label>
278
+ def text(content)
279
+ @nodes << TextNode.new(content)
280
+ self
281
+ end
282
+
283
+ # @since 0.2.5
284
+ # @api private
285
+ alias_method :+, :text
286
+
256
287
  # Resolves all the nodes and generates the markup
257
288
  #
258
289
  # @return [Lotus::Utils::Escape::SafeString] the output
@@ -0,0 +1,33 @@
1
+ module Lotus
2
+ module Helpers
3
+ module HtmlHelper
4
+ # Text node. Allows for text to be inserted between HTML tags.
5
+ #
6
+ # @since 0.2.5
7
+ # @api private
8
+ class TextNode
9
+ # Initialize a new text node
10
+ #
11
+ # @param content [String,#to_s] The content to be added.
12
+ #
13
+ # @return [Lotus::Helpers::HtmlHelper::TextNode]
14
+ #
15
+ # @since 0.2.5
16
+ # @api private
17
+ def initialize(content)
18
+ @content = content.to_s
19
+ end
20
+
21
+ # Resolve and return the output
22
+ #
23
+ # @return [String] the output
24
+ #
25
+ # @since 0.2.5
26
+ # @api private
27
+ def to_s
28
+ Utils::Escape.html(@content)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,23 +4,130 @@ module Lotus
4
4
  module Helpers
5
5
  # LinkTo Helper
6
6
  #
7
- # Including <tt>Lotus::Helpers::LinkTo</tt> will include the `link_to` method.
7
+ # Including <tt>Lotus::Helpers::LinkTo</tt> will include the
8
+ # <tt>link_to</tt> public method.
8
9
  #
9
- # @since x.x.x
10
+ # This helper can be used both in views and templates.
10
11
  #
11
- # @see Lotus::Helpers::HtmlHelper#html
12
- #
13
- # @example Usage
14
- # # 1
15
- # link_to('home', '/') # => <a href="/">home</a>
16
- #
17
- # # 2
18
- # link_to('users', :users_path, class: 'my_users') # => <a href="/users" class="my_users">users</div>
12
+ # @since 0.2.0
19
13
  module LinkToHelper
20
14
  include Lotus::Helpers::HtmlHelper
21
15
 
22
- def link_to(content, url, options = {}, &blk)
23
- options[:href] = url
16
+ # Generates an anchor tag for the given arguments.
17
+ #
18
+ # Contents are automatically escaped.
19
+ #
20
+ # @overload link_to(content, url, options)
21
+ # Use string as content
22
+ # @param content [String] content used in the a tag
23
+ # @param url [String] url used in href attribute
24
+ # @param options [Hash] HTML attributes to pass to the a tag
25
+ #
26
+ # @overload link_to(url, options, &blk)
27
+ # Use block as content
28
+ # @param url [String] url used in href attribute
29
+ # @param options [Hash] HTML attributes to pass to the a tag
30
+ # @param blk [Proc] A block that describes the contents of the a tag
31
+ #
32
+ # @return [String] HTML markup for the link
33
+ #
34
+ # @raise [ArgumentError] if the signature isn't respected
35
+ #
36
+ # @since 0.2.0
37
+ #
38
+ # @see Lotus::Helpers::HtmlHelper#html
39
+ #
40
+ # @example Both content and URL are strings
41
+ # <%= link_to('Home', '/') %>
42
+ # # => <a href="/">Home</a>
43
+ #
44
+ # @example Content string with route helper
45
+ # <%= link_to('Home', routes.path(:home)) %>
46
+ # # => <a href="/">Home</a>
47
+ #
48
+ # @example HTML attributes
49
+ # <%= link_to('Home', routes.path(:home), class: 'button') %>
50
+ # # => <a href="/" class="button">Home</a>
51
+ #
52
+ # @example Automatic content escape (XSS protection)
53
+ # <%= link_to(%(<script>alert('xss')</script>), '/') %>
54
+ # # => <a href="/">&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</a>
55
+ #
56
+ # @example Automatic content block escape (XSS protection)
57
+ # <%=
58
+ # link_to('/') do
59
+ # %(<script>alert('xss')</script>)
60
+ # end
61
+ # %>
62
+ # # => <a href="/">\n&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;\n</a>
63
+ #
64
+ # @example Content as block with string URL
65
+ # <%=
66
+ # link_to('/') do
67
+ # 'Home'
68
+ # end
69
+ # %>
70
+ # # => <a href="/">Home</a>
71
+ #
72
+ # @example Content as block
73
+ # <%=
74
+ # link_to(routes.path(:home)) do
75
+ # 'Home'
76
+ # end
77
+ # %>
78
+ # # => <a href="/">Home</a>
79
+ #
80
+ # @example Content as block with HTML attributes
81
+ # <%=
82
+ # link_to(routes.path(:home), id: 'home-link') do
83
+ # 'Home'
84
+ # end
85
+ # %>
86
+ # # => <a href="/" id: 'home-link'>Home</a>
87
+ #
88
+ # @example Content as HTML builder block
89
+ # <%=
90
+ # link_to(routes.path(:home)) do
91
+ # strong 'Home'
92
+ # end
93
+ # %>
94
+ # # => <a href="/"><strong>Home</strong></a>
95
+ #
96
+ # @example Provides both content as first argument and block
97
+ # <%=
98
+ # link_to('Home', routes.path(:home)) do
99
+ # strong 'Home'
100
+ # end
101
+ # %>
102
+ # # => ArgumentError
103
+ #
104
+ # @example Without any argument
105
+ # <%= link_to %>
106
+ # # => ArgumentError
107
+ #
108
+ # @example Without any argument and empty block
109
+ # <%=
110
+ # link_to do
111
+ # end
112
+ # %>
113
+ # # => ArgumentError
114
+ #
115
+ # @example With only content
116
+ # <%= link_to 'Home' %>
117
+ # # => ArgumentError
118
+ def link_to(content, url = nil, options = {}, &blk)
119
+ if block_given?
120
+ options = url || {}
121
+ url = content
122
+ content = nil
123
+ end
124
+
125
+ begin
126
+ options[:href] = url or raise ArgumentError
127
+ rescue TypeError
128
+ raise ArgumentError
129
+ end
130
+
24
131
  html.a(blk || content, options).to_s
25
132
  end
26
133
  end
@@ -8,7 +8,7 @@ module Lotus
8
8
  # By including <tt>Lotus::Helpers::NumberFormattingHelper</tt> it will
9
9
  # inject private method: <tt>format_number</tt>.
10
10
  #
11
- # @since x.x.x
11
+ # @since 0.2.0
12
12
  module NumberFormattingHelper
13
13
  private
14
14
  # Format the given number, according to the options
@@ -29,10 +29,10 @@ module Lotus
29
29
  #
30
30
  # @raise [TypeError] if number can't be formatted
31
31
  #
32
- # @since x.x.x
32
+ # @since 0.2.0
33
33
  #
34
34
  # @example
35
- # require 'lotus/helpers/number_formatter_helper'
35
+ # require 'lotus/helpers/number_formatting_helper'
36
36
  #
37
37
  # class Checkout
38
38
  # include Lotus::Helpers::NumberFormattingHelper
@@ -66,14 +66,14 @@ module Lotus
66
66
 
67
67
  # Formatter
68
68
  #
69
- # @since x.x.x
69
+ # @since 0.2.0
70
70
  # @api private
71
71
  class Formatter
72
72
  # Regex to delimitate integer part of a number
73
73
  #
74
74
  # @return [Regexp] the delimitation regex
75
75
  #
76
- # @since x.x.x
76
+ # @since 0.2.0
77
77
  # @api private
78
78
  #
79
79
  # @see Lotus::Helpers::NumberFormatter::Formatter#delimitate
@@ -83,7 +83,7 @@ module Lotus
83
83
  #
84
84
  # @return [Regexp] the guessing regex
85
85
  #
86
- # @since x.x.x
86
+ # @since 0.2.0
87
87
  # @api private
88
88
  #
89
89
  # @see Lotus::Helpers::NumberFormatter::Formatter#to_number
@@ -93,7 +93,7 @@ module Lotus
93
93
  #
94
94
  # @return [String] default separator
95
95
  #
96
- # @since x.x.x
96
+ # @since 0.2.0
97
97
  # @api private
98
98
  DEFAULT_SEPARATOR = '.'.freeze
99
99
 
@@ -101,7 +101,7 @@ module Lotus
101
101
  #
102
102
  # @return [String] default delimiter
103
103
  #
104
- # @since x.x.x
104
+ # @since 0.2.0
105
105
  # @api private
106
106
  DEFAULT_DELIMITER = ','.freeze
107
107
 
@@ -109,7 +109,7 @@ module Lotus
109
109
  #
110
110
  # @return [String] default precision
111
111
  #
112
- # @since x.x.x
112
+ # @since 0.2.0
113
113
  # @api private
114
114
  DEFAULT_PRECISION = 2
115
115
 
@@ -121,7 +121,7 @@ module Lotus
121
121
  # @option options [String] :separator fractional part delimiter
122
122
  # @option options [Integer] :precision rounding precision
123
123
  #
124
- # @since x.x.x
124
+ # @since 0.2.0
125
125
  # @api private
126
126
  #
127
127
  # @see Lotus::Helpers::NumberFormatter::Formatter::DEFAULT_DELIMITER
@@ -140,7 +140,7 @@ module Lotus
140
140
  #
141
141
  # @raise [TypeError] if number can't be formatted
142
142
  #
143
- # @since x.x.x
143
+ # @since 0.2.0
144
144
  # @api private
145
145
  def format
146
146
  parts.join(@separator)
@@ -152,7 +152,7 @@ module Lotus
152
152
  #
153
153
  # @return [Array] parts
154
154
  #
155
- # @since x.x.x
155
+ # @since 0.2.0
156
156
  # @api private
157
157
  def parts
158
158
  integer_part, fractional_part = to_str.split(DEFAULT_SEPARATOR)
@@ -163,7 +163,7 @@ module Lotus
163
163
  #
164
164
  # @return [String] delimitated string
165
165
  #
166
- # @since x.x.x
166
+ # @since 0.2.0
167
167
  # @api private
168
168
  def delimitate(part)
169
169
  part.gsub(DELIMITATION_REGEX) { |digit| "#{digit}#{@delimiter}" }
@@ -175,7 +175,7 @@ module Lotus
175
175
  #
176
176
  # @raise [TypeError] if number can't be formatted
177
177
  #
178
- # @since x.x.x
178
+ # @since 0.2.0
179
179
  # @api private
180
180
  def to_str
181
181
  to_number.to_s
@@ -187,7 +187,7 @@ module Lotus
187
187
  #
188
188
  # @raise [TypeError] if number can't be formatted
189
189
  #
190
- # @since x.x.x
190
+ # @since 0.2.0
191
191
  # @api private
192
192
  def to_number
193
193
  case @number
@@ -205,7 +205,7 @@ module Lotus
205
205
  #
206
206
  # @return [Float,Complex,Rational,BigDecimal] rounded number, if applicable
207
207
  #
208
- # @since x.x.x
208
+ # @since 0.2.0
209
209
  # @api private
210
210
  def rounded_number
211
211
  if @number.respond_to?(:round)
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Define version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.2.0'.freeze
6
+ VERSION = '0.2.5'.freeze
7
7
  end
8
8
  end
@@ -6,8 +6,8 @@ require 'lotus/helpers/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'lotus-helpers'
8
8
  spec.version = Lotus::Helpers::VERSION
9
- spec.authors = ['Luca Guidi', 'Trung Lê']
10
- spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com']
9
+ spec.authors = ['Luca Guidi', 'Trung Lê', 'Alfonso Uceda']
10
+ spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com', 'uceda73@gmail.com']
11
11
  spec.summary = %q{Lotus helpers}
12
12
  spec.description = %q{View helpers for Ruby applications}
13
13
  spec.homepage = 'http://lotusrb.org'
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
  spec.required_ruby_version = '>= 2.0.0'
21
21
 
22
- spec.add_dependency 'lotus-utils', '~> 0.5'
22
+ spec.add_dependency 'lotus-utils', '~> 0.5', '>= 0.5.2'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.6'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  - Trung Lê
9
+ - Alfonso Uceda
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2015-06-23 00:00:00.000000000 Z
13
+ date: 2015-09-30 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: lotus-utils
@@ -18,6 +19,9 @@ dependencies:
18
19
  - - "~>"
19
20
  - !ruby/object:Gem::Version
20
21
  version: '0.5'
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.5.2
21
25
  type: :runtime
22
26
  prerelease: false
23
27
  version_requirements: !ruby/object:Gem::Requirement
@@ -25,6 +29,9 @@ dependencies:
25
29
  - - "~>"
26
30
  - !ruby/object:Gem::Version
27
31
  version: '0.5'
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.5.2
28
35
  - !ruby/object:Gem::Dependency
29
36
  name: bundler
30
37
  requirement: !ruby/object:Gem::Requirement
@@ -71,12 +78,14 @@ description: View helpers for Ruby applications
71
78
  email:
72
79
  - me@lucaguidi.com
73
80
  - trung.le@ruby-journal.com
81
+ - uceda73@gmail.com
74
82
  executables: []
75
83
  extensions: []
76
84
  extra_rdoc_files: []
77
85
  files:
78
86
  - LICENSE.md
79
87
  - README.md
88
+ - lib/lotus-helpers.rb
80
89
  - lib/lotus/helpers.rb
81
90
  - lib/lotus/helpers/escape_helper.rb
82
91
  - lib/lotus/helpers/form_helper.rb
@@ -87,6 +96,7 @@ files:
87
96
  - lib/lotus/helpers/html_helper/empty_html_node.rb
88
97
  - lib/lotus/helpers/html_helper/html_builder.rb
89
98
  - lib/lotus/helpers/html_helper/html_node.rb
99
+ - lib/lotus/helpers/html_helper/text_node.rb
90
100
  - lib/lotus/helpers/link_to_helper.rb
91
101
  - lib/lotus/helpers/number_formatting_helper.rb
92
102
  - lib/lotus/helpers/routing_helper.rb
@@ -112,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
122
  version: '0'
113
123
  requirements: []
114
124
  rubyforge_project:
115
- rubygems_version: 2.4.8
125
+ rubygems_version: 2.4.5.1
116
126
  signing_key:
117
127
  specification_version: 4
118
128
  summary: Lotus helpers