kiss 1.8.2 → 1.8.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ spec = Gem::Specification.new do |s|
7
7
  s.name = 'kiss'
8
8
  s.version = File.read('VERSION').chomp
9
9
  s.author = 'Shawn Van Ittersum'
10
- s.email = 'shawn @nospam@ multiwidget.com'
10
+ s.email = 'shawn @nospam@ appjudo.com'
11
11
  s.homepage = 'http://www.rubykiss.org'
12
12
  s.rubyforge_project = 'kiss'
13
13
  s.summary = 'An MVC web application framework using Rack, Sequel, and Erubis.'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.8.2
1
+ 1.8.3
data/data/scaffold.tgz CHANGED
Binary file
@@ -194,10 +194,6 @@ class Kiss
194
194
  @_currency.to_s + input_tag_html(attrs) + tip_html(attrs)
195
195
  end
196
196
 
197
- def table_row_html
198
- form.component_html(self)
199
- end
200
-
201
197
  def tip_html(attrs)
202
198
  (tip = attrs.has_key?(:tip) ? attrs[:tip] : @_tip) ? " <small>#{tip}</small>" : ''
203
199
  end
data/lib/kiss/form.rb CHANGED
@@ -42,24 +42,54 @@ class Kiss
42
42
  @_form = form
43
43
  @_components = []
44
44
  end
45
+
46
+ # Renders HTML for specified form field.
47
+ def component_table_row_html(field)
48
+ field = fields[field.to_s] if (field.is_a?(Symbol) || field.is_a?(String))
49
+ return field.element_html if field.is_a?(HiddenField)
50
+
51
+ type = field.class.type
52
+ prompt = field.prompt
53
+ label = field.label
54
+ errors = field.errors_html
55
+ required = field.required ? %Q(<span class="kiss_form_required">#{@_form.mark_required}</span> ) : ''
56
+
57
+ parts = [
58
+ prompt ? %Q(<tr class="kiss_form_prompt"><td class="kiss_form_label">#{required}</td><td>#{prompt.to_s}</td></tr>) : '',
59
+ %Q(<tr class="kiss_form_#{type}"><td class="kiss_form_label#{errors ? ' error' : ''}">),
60
+ !prompt ? (required + (label.blank? ? '' : label.to_s + ':' )) : '',
61
+ %Q(</td><td class="kiss_form_#{type}">),
62
+ field.element_html, "</td></tr>"
63
+ ]
64
+ if errors
65
+ parts += [
66
+ '<tr class="kiss_form_error_row"><td class="kiss_form_required"></td><td>',
67
+ errors,
68
+ '</td></tr>'
69
+ ]
70
+ end
71
+ parts.join
72
+ end
45
73
 
46
74
  # Renders HTML for form fields.
47
75
  def components_html
48
76
  @_components.map do |component|
49
- form.component_html(component)
77
+ component_table_row_html(component)
50
78
  end.join
51
79
  end
52
80
  alias_method :fields_html, :components_html
53
81
 
54
82
  # Renders open of form table.
55
- def table_html_open
83
+ def table_opening_html
56
84
  %Q(<table class="kiss_form_column" border=0 cellspacing=0><tbody>)
57
85
  end
86
+ alias_method :table_html_open, :table_opening_html
58
87
 
59
88
  # Renders close of form table.
60
- def table_html_close
89
+ def table_closing_html
61
90
  '</tbody></table>'
62
91
  end
92
+ alias_method :table_html_close, :table_closing_html
63
93
 
64
94
  def html
65
95
  [
@@ -314,23 +344,61 @@ class Kiss
314
344
  end
315
345
 
316
346
  def validate_uniqueness_of(column_set)
317
- column_set_labels_values = column_set.map do |column_name|
318
- (field = fields[column_name.to_s]) ?
319
- [field.label, field.value] :
320
- [column_name.to_s.sub(/_id\Z/, '').titlecase, object[column_name]]
347
+ conditions = column_set.last.is_a?(Hash) ? column_set.pop : {}
348
+
349
+ nouns = []
350
+ true_adjectives = []
351
+ false_adjectives = []
352
+
353
+ column_set.each do |column_name|
354
+ key = column_name.to_sym
355
+ column_name = column_name.to_s
356
+
357
+ field = fields[column_name]
358
+ label, value = field ?
359
+ [field.label.downcase, field.value] :
360
+ [column_name.sub(/_id\Z/, '').downcase, object[column_name.to_sym]]
361
+ conditions[key] = value
362
+
363
+ if column_name =~ /\Ais_/ || column_name =~ /_at\Z/
364
+ if value.zero? || value.nil?
365
+ false_adjectives
366
+ else
367
+ true_adjectives
368
+ end
369
+ else
370
+ nouns
371
+ end << label
321
372
  end
322
- conditions = column_set.zip column_set_labels_values.map {|lv| lv[1] }
323
373
 
324
374
  dataset = @_object.model.filter(conditions)
325
- unless (@_object.new? ? dataset : dataset.exclude(@_object.pk_hash)).empty?
326
- labels = column_set_labels_values.map {|lv| lv[0] }
327
- message = "There is already another #{@_object.model.name.singularize.gsub('_', ' ')} with the same #{labels.conjoin}."
328
- return (
329
- (column_set.length == 1) &&
330
- (field_name = column_set[0].to_str) &&
331
- fields[field_name]
332
- ) ? add_error(field_name, message) : add_error(message)
375
+ dataset = dataset.exclude(@_object.pk_hash) unless @_object.new?
376
+ return nil if dataset.empty?
377
+
378
+ true_adjective_string = true_adjectives.empty? ? '' :
379
+ (true_adjectives.join(', ') + ' ')
380
+ humanized_object_class_name = @_object.model.name.singularize.gsub('_', ' ')
381
+ false_adjective_string = false_adjectives.empty? ? '' :
382
+ ", not #{false_adjectives.conjoin('or')},"
383
+ with_clause = nouns.empty? ? '' : " with the same #{nouns.conjoin}"
384
+ message = [
385
+ 'There is already another ',
386
+ true_adjective_string,
387
+ humanized_object_class_name,
388
+ false_adjective_string,
389
+ with_clause,
390
+ '.'
391
+ ].join
392
+
393
+ # Add error to a single field if appropriate.
394
+ if column_set.length == 1
395
+ field_name = column_set[0].to_str
396
+ if field_name && fields[field_name]
397
+ return add_error(field_name, message)
398
+ end
333
399
  end
400
+ # Else add to top of form.
401
+ return add_error(message)
334
402
  end
335
403
 
336
404
  # Checks whether form was submitted and accepted by user and, if so,
@@ -467,6 +535,11 @@ class Kiss
467
535
  end
468
536
  alias_method :accepted, :accepted?
469
537
 
538
+ # Renders current action using form's HTML as action render content.
539
+ def render(options = {})
540
+ @_delegate.render options.merge(:content => html)
541
+ end
542
+
470
543
  # Renders error HTML block for top of form, and returns as string.
471
544
  def errors_html
472
545
  return nil unless has_errors?
@@ -485,35 +558,64 @@ class Kiss
485
558
  %Q(<table class="kiss_form_errors"><tr><td>#{content}</td></tr></table>)
486
559
  end
487
560
 
488
- # Renders current action using form's HTML as action render content.
489
- def render(options = {})
490
- @_delegate.render options.merge(:content => html)
491
- end
492
-
493
561
  def form_name_hidden_tag_html
494
562
  %Q(<input type=hidden name="form" value="#{@_name}">)
495
563
  end
496
564
 
497
- # Renders beginning of form (form open tag and form/field/error styles).
498
- def html_open
499
- @_error_class ||= 'kiss_form_error_message'
500
- @_field_error_class ||= @_error_class
501
-
502
- # form tag
503
- form_attrs = ['id', 'method', 'enctype', 'class', 'style'].map do |attr|
504
- next if (value = send attr).blank?
505
- "#{attr}=\"#{send attr}\""
506
- end
507
- if @_html
508
- @_html.each_pair do |k, v|
509
- form_attrs.push("#{k}=\"#{v}\"")
510
- end
565
+ # Renders HTML for form sections.
566
+ def sections_html
567
+ sections.map do |section|
568
+ section.html
569
+ end.join
570
+ end
571
+ alias_method :components_html, :sections_html
572
+ alias_method :fields_html, :sections_html
573
+
574
+ def required_legend_html
575
+ (@_has_required_fields && @_mark_required) ?
576
+ %Q( <tr><td class="kiss_form_help">Required fields marked by <span class="kiss_form_required">*</span></td></tr> ) : ''
577
+ end
578
+
579
+ # Renders form submit buttons.
580
+ def submit_html
581
+ if @_submit
582
+ prompt = @_submit.prompt ? (@_submit.prompt + '<br/>') : ''
583
+ [
584
+ '<tr class="kiss_form_submit"><td>',
585
+ prompt,
586
+ @_submit.element_html,
587
+ '<br/></td></tr>'
588
+ ].join
589
+ else
590
+ ''
511
591
  end
512
- form_tag = %Q(<form action="#{@_action}" #{form_attrs.join(' ')}>#{form_name_hidden_tag_html})
513
-
514
- # style tag
592
+ end
593
+
594
+ # Renders open of form table.
595
+ def table_opening_html
596
+ %Q(<table class="kiss_form" border=0 cellspacing=0><tbody>)
597
+ end
598
+ alias_method :table_html_open, :table_opening_html
599
+
600
+ # Renders close of form table.
601
+ def table_closing_html
602
+ '</tbody></table>'
603
+ end
604
+ alias_method :table_html_close, :table_closing_html
605
+
606
+ def table_html
607
+ [
608
+ table_opening_html,
609
+ required_legend_html,
610
+ sections_html,
611
+ submit_html,
612
+ table_closing_html
613
+ ].flatten.join
614
+ end
615
+
616
+ def style_html
515
617
  styles = []
516
- styles.push( <<-EOT
618
+ styles << <<-EOT
517
619
  table.kiss_form {
518
620
  border: 0;
519
621
  border-collapse: collapse;
@@ -610,10 +712,9 @@ table.kiss_form_column > tbody > tr > td {
610
712
  text-align: center;
611
713
  }
612
714
  EOT
613
- )
614
715
 
615
716
  if @_error_class == 'kiss_form_error_message'
616
- styles.push( <<-EOT
717
+ styles << <<-EOT
617
718
  .kiss_form_error_message {
618
719
  padding: 1px 4px;
619
720
  border: 1px solid #db4;
@@ -643,91 +744,47 @@ tr.kiss_form_error_row .kiss_form_error_message {
643
744
  margin-bottom: 0;
644
745
  }
645
746
  EOT
646
- )
647
747
  end
648
- style_tag = styles.size == 0 ? '' : "<style>" + styles.join('') + "</style>"
748
+ styles.empty? ? '' : ["<style>", styles, "</style>"].flatten.join
749
+ end
750
+
751
+ # Renders beginning of form (form open tag and form/field/error styles).
752
+ def opening_html
753
+ @_error_class ||= 'kiss_form_error_message'
754
+ @_field_error_class ||= @_error_class
755
+
756
+ # form tag
757
+ form_attrs = ['id', 'method', 'enctype', 'class', 'style'].map do |attr|
758
+ next if (value = send attr).blank?
759
+ "#{attr}=\"#{send attr}\""
760
+ end
761
+ if @_html
762
+ @_html.each_pair do |k, v|
763
+ form_attrs.push("#{k}=\"#{v}\"")
764
+ end
765
+ end
766
+ form_tag = %Q(<form action="#{@_action}" #{form_attrs.join(' ')}>#{form_name_hidden_tag_html})
649
767
 
650
768
  # combine
651
- return %Q(#{@_prepend_html}#{form_tag}#{style_tag}<div class="kiss_form">#{errors_html})
769
+ [
770
+ style_html,
771
+ @_prepend_html,
772
+ form_tag,
773
+ '<div class="kiss_form">',
774
+ errors_html
775
+ ].join
652
776
  end
777
+ alias_method :html_open, :opening_html
653
778
 
654
779
  # Renders end of form (form close tag).
655
- def html_close
656
- "</div></form>#{@_append_html}"
657
- end
658
-
659
- # Renders HTML for form sections.
660
- def sections_html
661
- sections.map do |section|
662
- section.html
663
- end.join
664
- end
665
-
666
- # Renders HTML for form fields.
667
- def components_html
668
- @_components.map do |component|
669
- component_html(component)
670
- end.join
671
- end
672
- alias_method :fields_html, :components_html
673
-
674
- # Renders open of form table.
675
- def table_html_open
676
- %Q(<table class="kiss_form" border=0 cellspacing=0><tbody>)
677
- end
678
-
679
- def required_legend_html
680
- (@_has_required_fields && @_mark_required) ?
681
- %Q( <tr><td class="kiss_form_help">Required fields marked by <span class="kiss_form_required">*</span></td></tr> ) : ''
682
- end
683
-
684
- # Renders close of form table.
685
- def table_html_close
686
- '</tbody></table>'
687
- end
688
-
689
- # Renders form submit buttons.
690
- def submit_html
691
- @_submit ? %Q(<tr class="kiss_form_submit"><td>#{@_submit.element_html}</td></tr>) : ''
780
+ def closing_html
781
+ '</div></form>' + @_append_html
692
782
  end
783
+ alias_method :html_close, :closing_html
693
784
 
694
785
  # Renders complete form HTML.
695
786
  def html
696
- return [
697
- html_open,
698
- table_html_open,
699
- required_legend_html,
700
- sections_html,
701
- submit_html,
702
- table_html_close,
703
- html_close
704
- ].flatten.join
787
+ [opening_html, table_html, closing_html].join
705
788
  end
706
-
707
- # Renders HTML for specified form field.
708
- def component_html(field)
709
- field = fields[field.to_s] if (field.is_a?(Symbol) || field.is_a?(String))
710
- return field.element_html if field.is_a?(HiddenField)
711
-
712
- type = field.class.type
713
- prompt = field.prompt
714
- label = field.label
715
- errors = field.errors_html
716
- required = field.required ? %Q(<span class="kiss_form_required">#{@_mark_required}</span> ) : ''
717
-
718
- ([
719
- prompt ? %Q(<tr class="kiss_form_prompt"><td class="kiss_form_label">#{required}</td><td>#{prompt.to_s}</td></tr>) : '',
720
-
721
- %Q(<tr class="kiss_form_#{type}"><td class="kiss_form_label#{errors ? ' error' : ''}">),
722
- !prompt ? (required + (label.blank? ? '' : label.to_s + ':' )) : '',
723
- %Q(</td><td class="kiss_form_#{type}">),
724
- field.element_html, "</td></tr>"
725
- ] + (errors ? [
726
- '<tr class="kiss_form_error_row"><td class="kiss_form_required"></td><td>',
727
- errors,
728
- '</td></tr>'
729
- ] : [])).join
730
- end
731
- alias_method :field_html, :component_html
732
789
  end
733
790
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 8
8
- - 2
9
- version: 1.8.2
8
+ - 3
9
+ version: 1.8.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Shawn Van Ittersum
@@ -82,7 +82,7 @@ dependencies:
82
82
  type: :runtime
83
83
  version_requirements: *id005
84
84
  description:
85
- email: shawn @nospam@ multiwidget.com
85
+ email: shawn @nospam@ appjudo.com
86
86
  executables:
87
87
  - kiss
88
88
  extensions: []