-
Notifications
You must be signed in to change notification settings - Fork 41
/
paragraph_feature.rb
1962 lines (1679 loc) · 69.8 KB
/
paragraph_feature.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2009 Pascal Rettig.
require 'radius'
=begin rdoc
ParagraphFeatures's are used to render paragraph output
in a highly user-customizable way
They are the view part of the trinty of classes used for
configuring and rendering paragraphs (ParagraphController and ParagraphRenderer
are the other two).
ParagraphFeature's generally define a number of features using the ParagraphFeature#feature class method
and then define a method called [feature_name]_feature(data) that actually renders the feature.
=== Example
For example, a (abbreviated) feature for the poll demo might look like:
class PollDemo::PageFeature < ParagraphFeature
feature :poll_demo_page_view, :default_feature => <<-FEATURE
<cms:poll>
<cms:responded><cms:graph/></cms:responded>
<cms:form>
<b><cms:question/></b><br/>
<cms:response/><br/><cms:submit/>
</cms:form>
</cms:poll>
<cms:no_poll>Invalid Poll</cms:no_poll>
FEATURE
def poll_demo_page_view_feature(data)
webiva_feature(:poll_demo_page_view,data) do |c|
c.expansion_tag('poll') { |t| data[:poll] }
c.expansion_tag('responded') { |t| data[:state] == 'responded'}
c.value_tag('responded:graph') do |t|
data[:poll].results_graph(data[:options].graph_width,
data[:options].graph_height)
end
c.form_for_tag('form','poll') do |t|
if data[:state] == 'question'
{
:object => data[:response],
:code => hidden_field_tag('poll[poll_demo_poll_id]',data[:poll].id)
}
end
end
c.h_tag('question') { |t| data[:poll].question }
c.field_tag('form:response',
:control => :radio_buttons,
:separator => '<br/>') { |t| data[:poll].answer_options }
c.submit_tag('form:submit',:default => 'Submit')
end
end
...
end
=== Naming Convention
A couple of things to note, first the feature names aren't namespaced, so you need to
manually name space them. The standard method is [module_name]_[renderer_name]_[paragraph_name]
In the case about the module's name is "poll_demo", the renderer name is "page" and the paragraph
name is "view", so the feature is called :poll_demo_page_view.
== webiva_feature and webiva_custom_feature
Inside of the actual feature method is usually a call to webiva_feature or webiva_custom_feature,
which yields a context, inside of which you can define tags for the feature to use. The end
result of the webiva_feature (or webiva_custom_feature) call is the html that the feature renders,
using either the default feature or the customized feature defined by the site integrator.
When should you use which? Well, if you the tags that you define don't depend on the data variable
being passed in, then you can use webiva_feature. If they do depend on the data (for example in a custom
content model publication you'll have different tags depending on the content mode) You should
use webiva_custom_feature so the system knows not to cache the generated tags.
Note: the caching is planned for the future, so using the correct call is only important for
future proofing your module
=== Defining Tags: Basic Radius
The "c" that webiva_feature passes to the block is called a FeatureContext. You can use it to define
tags that will be available in the feature. Webiva uses the excellent (Radius Gem)[http://github.com/jlong/radius]
to render features and the webiva class ParagraphFeature::FeatureContext inherits from Radius::Context.
At the most basic level you can define tags using 'define_tag' - which is the standard radius method.
For example to create a tag that will be available as "<cms:name/>" in your ParagraphFeature you could write:
c.define_tag('name') { |t| h(data[:object].name) }
If you wanted to be able to write something like: "<cms:object>We have an object!</cms:object>" that only
displays the text "We have an object!" you could define a tag like:
c.define_tag('object') { |t| data[:object] ? t.expand : nil }
Radius also supports some local variables and nested tags. For example the above two tags could be written
like:
c.define_tag('object') do |t|
t.locals.object = data[:object]
t.locals.object ? t.expand : nil
end
c.define_tag('object:name') { |t| h(t.locals.object.name) }
This would allow a site feature like "<cms:object>Name: <cms:name/></cms:object>" which would only
display: "Name: .." if the object existed, and only allow the use of the <cms:name/> tag inside of
a <cms:object/> tag.
=== FeatureContext Tags
Writing define_tag and a whole bunch of display logic gets old quickly, so FeatureContext adds
a whole slew of tags that you can use in lieu of define_tag that add one or more tag with
specific functionality. The above could also be written:
c.expansion_tag('object') { |t| t.locals.object = data[:object] }
c.h_tag('object:name') { |t| t.locals.object.name }
See ParagraphFeature::FeatureContext for more information.
=== Usings features
Features are made available in ParagraphRenderer's by calling the 'feature /module/feature' class
method and then either calling the feature directly via it's method name or by calling:
render_paragraph :feature => :feature_name
See ParagraphRenderer for more information.
=end
class ParagraphFeature
# Include some helpers needed for
# rendering pages
include ActionView::Helpers::TextHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormHelper
include ERB::Util
include EscapeHelper
include ActionView::Helpers::FormTagHelper
attr_reader :renderer
def initialize(paragraph,renderer) #:nodoc:
@para = paragraph
@renderer = renderer
end
def method_missing(method,*args) #:nodoc:
if method.to_s =~ /feature$/
raise 'Undefined feature:' + method.to_s
else
if args.length > 0
@renderer.send(method,*args)
else
@renderer.send(method)
end
end
end
# Creates a new standalone feature that can be called with a
# data hash directly.
def self.standalone_feature(site_feature_id=nil)
self.new(PageParagraph.new(:site_feature_id => site_feature_id),dummy_renderer)
end
# Parses an inline feature
def parse_inline(src,&block)
parser_context = FeatureContext.new(self) do |c|
c.define_position_tags
yield c
end
parse_feature(SiteFeature.new(:body => src),parser_context)
end
def self.dummy_renderer(controller=nil) #:nodoc:
ParagraphRenderer.new(UserClass.domain_user_class,controller || ApplicationController.new,PageParagraph.new,SiteNode.new,PageRevision.new)
end
# Returns a list of tags that are defined in this feature
def self.document_feature(name,data={},controller=nil,publication=nil)
rnd = self.dummy_renderer(controller)
feature = self.new(PageParagraph.new,rnd)
feature.set_documentation(true)
feature.send(name,data)
end
# Registers a feature named type.
# Should be called with :default_feature option to show the markup for
# the default feature (otherwise by nothing will render)
def self.feature(type,opts = {})
features = self.available_features
features << type
sing = class << self; self; end
sing.send :define_method, :available_features do
return features
end
default_feature = opts.delete(:default_feature) || ''
sing.send :define_method, "get_default_feature_#{type.to_s}" do
default_feature
end
sing.send :define_method, "get_feature_options_#{type.to_s}" do
opts
end
end
=begin rdoc
FeatureContext is the context passed to site features when they call ParagraphFeathre#webiva_feature.
It includes most of the basic view helpers, allow you to call standard rails methods like
"tag" to generate an html tag.
It also defines a number of custom tags methods for Webiva. All custom tag
methods are aliased without the leading "define", so define_expansion_tag is
also available as expansion_tag.
Most of the Webiva custom tags accept a block that returns the object on which the tag should
act. For example - expansion_tag accepts a block and will only expand the tag if the value of
block is non-nil
=end
class FeatureContext < Radius::Context
include ActionView::Helpers::TagHelper
include EscapeMethods
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormHelper
include StyledFormBuilderGenerator::FormFor
include ActionView::Helpers::FormOptionsHelper
include PageHelper
include EscapeHelper
def url_for(opts) #:nodoc:
if opts.nil?
nil
elsif opts.is_a? String
opts
else
super opts
end
end
def initialize(renderer=nil) #:nodoc:
@renderer = renderer
super()
end
def method_missing(method,*args) #:nodoc:
if args.length > 0
@renderer.send(method,*args)
else
@renderer.send(method)
end
end
# Defines a set of tags of the form [name]:[tag_name] for each
# element of the array tag_names making it easy to expose a number
# of attributes in an object. For example if we had an object in
# t.locals.person with a first_name,last_name, and email attribute
# we could write:
#
# c.expansion_tag("person") { |t| t.locals.person = data[:person] }
# c.attribute_tags("person",["first_name","last_name","email"]) { |t| t.locals.person }
#
# which would add three tags:
#
# <cms:person:first_name/>, <cms:person:last_name/>, <cms:person:email_name/>
#
def define_attribute_tags(name,tag_names,&block)
tag_names.each do |tag_name|
define_attribute_tag_helper("#{name}:#{tag_name}",tag_name,block)
end
end
def define_attribute_tag_helper(name,tag_name,block) #:nodoc:
define_value_tag(name) do |t|
h(block.call(t).send(tag_name))
end
end
# Define a value tag and escape the output with a h call
#
# === Options
# [:format]
# Set to :simple to call Rail's simple_format method around the returned, escaped value
def define_h_tag(name,field='value',options={},&block)
case options[:format]
when :simple
define_value_tag(name,field,&Proc.new { |t| val = block.call(t); val ? simple_format(h(val)) : nil })
else
define_value_tag(name,field,&Proc.new { |t| val = block.call(t); val ? h(val) : nil })
end
end
def value_tag_helper(t,field,val) #:nodoc:
t.locals.send("#{field}=",val)
if t.attr['equals']
eql = val.is_a?(Integer) ? t.attr['equals'].to_i : t.attr['equals']
val == eql ? t.expand : nil
elsif t.attr['contains']
contains = t.attr['contains'].to_s.split(",").map { |elm| elm.strip.blank? ? nil : elm.strip }.compact
contains.include?(val) ? t.expand : nil
elsif t.attr['include']
inc = val.is_a?(Array) && val.include?(t.attr['include'])
inc ? t.expand : nil
elsif t.attr['not_equals']
eql = val.is_a?(Integer) ? t.attr['not_equals'].to_i : t.attr['not_equals']
val != eql ? t.expand : nil
elsif t.attr['min']
min = val.is_a?(Integer) ? t.attr['min'].to_i : tar.attr['min']
val >= min ? t.expand : nil
elsif t.attr['max']
max = val.is_a?(Integer) ? t.attr['max'].to_i : tar.attr['max']
val <= max ? t.expand : nil
elsif t.attr['link']
attributes = t.attr.clone
attributes.delete('link')
content_tag('a',t.expand, attributes.merge({ :href => val}))
else
if val.is_a?(Array)
val.length == 0 || val[0].blank? ? nil : t.expand
else
val.blank? ? nil : t.expand
end
end
end
def escape_value_helper(value,escape) #:nodoc:
case escape
when 'value':
vh(value)
when 'javascript':
jh(value)
when 'javascript_value':
jvh(value)
else
h(value)
end
end
# Defines a value tag, which is the standard tag that
# is used to output a piece of content. Using value_tag
# instead of just define_tag allows for some additional functionality
# including using the value tag as a block.
#
# For example, if you defined a value tag called 'name' you could write:
#
# <cms:name>Name: <cms:value/></cms:name>
#
# Which would only display the Name: .. text if the return value of the block
# isn't blank.
#
# You can pass an "escape" attribute set to html, value, javascript or javascript_value
# to escape the output of a tag as necessary
#
# You can also use the value as an expansion tag by passing any of the following:
# max="MAX VALUE", min="MIN_VALUE", not_equals="COMPARE VALUE",
# equals="COMPARE VALUE", contains="Multiple,Comma,Separated,Values"
#
# For example:
#
# <cms:age min='21'>Alcohol Reference</cms:age>
#
# Which would only expand if the value of age is greater than 21
#
def define_value_tag(name,field='value',&block)
define_tag(name) do |tag|
val = yield(tag)
if tag.single?
val = truncate(Util::TextFormatter.text_plain_generator(val), :limit => tag.attr['limit'].to_i, :omission => tag.attr['omission'] || '...') if tag.attr['limit']
if tag.attr['escape']
escape_value_helper(val,tag.attr['escape'])
else
val
end
else
value_tag_helper(tag,field,val)
end
end
name_parts = name.split(":")
name_parts[-1] = "no_" + name_parts[-1]
define_tag(name_parts.join(":")) do |tag|
val = block.call(tag)
val.blank? ? tag.expand : nil
end
define_tag(name + ":#{field}") do |tag|
tag.locals.send(field)
end
end
# Loop tags are used to loop over a list of items, assign each item to a local
# and then expand the interior of the tag.
# For example lets say you had a list of "posts" in data[:posts] that you wanted
# to iterate over, you could define a loop tag and a couple of attribute tags:
#
# c.loop_tag('post') { |t| data[:posts] }
# c.attribute_tags('post',%w(name body)) { |t| t.locals.post }
#
# This would create a number of tags:
#
# [<cms:posts>]
# Block tag that is expanded only if data[:posts] is non-nil and has a length > 0
# [<cms:no_posts> or <cms:not_posts>]
# Block tags that are expanded only if data[:posts] is nil or has a length == 0
# [<cms:post>]
# Block tag that is called once for each element of data[:posts], it will assign a local
# variable called t.locals.post that will contain the individual post
# [<cms:post:name/>]
# h tag that will display the name for each post
# [<cms:post:body/>]
# h that will display the body of each post
#
# An example code usage would be:
#
# <cms:posts>
# <div class='posts'>
# <cms:post>
# <div class='post'>
# <h2><cms:name/></h2>
# <cms:body/>
# </div>
# </cms:post>
# </div>
# </cms:posts>
# <cms:no_posts><h2>No Posts</h2></cms:no_posts>
def define_loop_tag(name,plural=nil,options = {})
name_parts = name.split(":")
name_base = options[:local] || name_parts[-1]
plural = name_base.pluralize unless plural
name_parts[-1] = plural
expansion_tag = name_parts.join(":")
define_tag(name) { |t| each_local_value(yield(t),t,name_base) }
define_expansion_tag(expansion_tag) { |t| arr = yield(t); arr && arr.length > 0 }
end
# Expansion tags are block tags that will expand the value of the tag
# if the block returns a non-nil value
# a common usage is to assign a local in the yielded block
#
# For example:
#
# c.expansion_tag('item') { |t| t.locals.item = data[:item] }
# ..Now defines some tags underneath item:, like item:name, item:price, etc...
#
def define_expansion_tag(name)
define_tag(name) do |tag|
yield(tag) ? tag.expand : nil
end
name_parts = name.split(":")
tg_name = name_parts[-1]
name_parts[-1] = "no_" + tg_name
define_tag(name_parts.join(":")) do |tag|
yield(tag) ? nil : tag.expand
end
name_parts[-1] = "not_" + tg_name
define_tag(name_parts.join(":")) do |tag|
yield(tag) ? nil : tag.expand
end
end
# Creates a tag that expects a date or time object to be returned by the yielded block.
#
# For example:
#
# c.date_tag('current_time') { |t| Time.now }
#
def define_date_tag(name,default_format = '%m/%d/%Y',&block)
define_value_tag(name) do |tag|
val = yield(tag)
if !val.is_a?(Time)
begin
val = Time.parse(val)
rescue Exception => e
#
end
end
val.localize(tag.attr['format'] || default_format) if val
end
end
def reset_output #:nodoc:
@output_buffer = ""
end
def concat(txt,binding=nil) #:nodoc:
@output_buffer += txt
end
attr_reader :output_buffer
# Creates an end user table, see EndUserTable for more information
# (EndUserTable's are the equivalent of ActiveTable but for the front end
# of the site)
def define_end_user_table_tag(tag_name,field,options = {},&block)
options.symbolize_keys!
define_tag(tag_name) do |t|
opts = options.clone.merge(t.attr.symbolize_keys.slice(:empty,:style,:width))
opts[:empty] ||= "No Entries"
t.locals.tbl = yield t
reset_output
end_user_table_for(t.locals.tbl,opts) do |row|
t.locals.send("#{field}=",row)
concat(t.expand)
end
output_buffer
end
define_tag("#{tag_name}:row") do |t|
"<tr #{t.locals.tbl.highlight_row(t.locals.send(field))}>" + t.expand + "</tr>"
end
define_tag("#{tag_name}:row:checkbox") do |t|
t.locals.tbl.entry_checkbox(t.locals.send(field))
end
end
# Helper method that defines a number of tags that provide information
# about the current user.
#
# user_tags('user') { |t| t.locals.user }
#
# Would define the following tags:
# [<cms:user>]
# Expansion tag that only expands if the yield block returns non-nil
# [<cms:user:logged_in>]
# Expansion tag that only expands if the user is logged in
# [<cms:user:name/>]
# h tag that returns the full name of the user
# [<cms:user:first_name/>]
# h tag the returns the users first name
# [<cms:user:last_name/>]
# h tag the returns the users last name
# [<cms:user:profile/>]
# value tag the returns the user's profile id
# [<cms:user:myself>]
# expansion tag that expands only if the user is the currently logged in user
def define_user_tags(tag_name,options={},&block)
local=options[:local]
if !local
name_parts = tag_name.split(":")
local = name_parts[-1]
end
define_expansion_tag(tag_name) { |t| block ? t.locals.send("#{local}=",block.call(t)) : t.locals.send(local) }
define_expansion_tag(tag_name + ":logged_in") { |t| usr = t.locals.send(local); !usr.id.blank? }
define_h_tag(tag_name + ":name") { |t| usr = t.locals.send(local); usr.name if usr }
define_h_tag(tag_name + ":username") { |t| usr = t.locals.send(local); usr.username if usr }
define_h_tag(tag_name + ":first_name") { |t| usr = t.locals.send(local); usr.first_name if usr }
define_h_tag(tag_name + ":last_name") { |t| usr = t.locals.send(local); usr.last_name if usr }
define_value_tag(tag_name + ":profile") { |t| usr = t.locals.send(local); usr.user_profile_id if usr }
define_value_tag(tag_name + ":profile_name") { |t| usr = t.locals.send(local); usr.user_profile.name if usr }
define_expansion_tag(tag_name + ":myself") { |t| usr = t.locals.send(local); usr == myself if usr }
define_image_tag(tag_name + ":img") { |t| usr = t.locals.send(local); usr.image if usr }
end
# Similar to define_user_tags but it defines a whole bunch of tags
# with detailed information about the user.
#
# the only option that is currently accepted is :local, which is the name
# of the local variable to find the user object (defaults to the name of the tag)
#
# expansion_tag('user') { |t| t.locals.user = data[:user] }
# user_details_tags('user', :local => 'user')
#
# Would define expansion tags for: myself,male,female,address,work_address
# and would define value tag for:
# user_id,first_name,last_name,salutation,name,email,cell_phone,img,second_img,fallback_img
# it also defines address tags (see #define_user_address_tags) for the address and work_address
def define_user_details_tags(name_base,options={})
local=options[:local]
if !local
name_parts = name_base.split(":")
local = name_parts[-1]
end
expansion_tag("#{name_base}:myself") { |t| t.locals.send(local) == myself }
expansion_tag("#{name_base}:male") { |t| t.locals.send(local).gender == 'm' }
expansion_tag("#{name_base}:female") { |t| t.locals.send(local).gender == 'f' }
define_value_tag("#{name_base}:user_id") { |t| t.locals.send(local).id }
define_value_tag("#{name_base}:profile") { |t| t.locals.send(local).user_profile_id }
define_value_tag("#{name_base}:profile_name") { |t| t.locals.send(local).user_profile.name }
define_h_tag("#{name_base}:first_name") { |t| t.locals.send(local).first_name }
define_h_tag("#{name_base}:last_name") { |t| t.locals.send(local).last_name }
define_h_tag("#{name_base}:salutation") { |t| t.locals.send(local).salutation }
define_h_tag("#{name_base}:name") { |t| t.locals.send(local).name }
define_h_tag("#{name_base}:email") { |t| t.locals.send(local).email }
define_h_tag("#{name_base}:cell_phone") { |t| t.locals.send(local).cell_phone }
define_image_tag("#{name_base}:img") { |t| t.locals.send(local).image }
define_image_tag("#{name_base}:second_img") { |t| t.locals.send(local).second_image }
define_image_tag("#{name_base}:fallback_img") { |t| t.locals.send(local).second_image || t.locals.send(local).image }
expansion_tag("#{name_base}:address") { |t| t.locals.address = t.locals.send(local).address }
define_user_address_tags("#{name_base}:address")
expansion_tag("#{name_base}:work_address") { |t| t.locals.work_address = t.locals.send(local).work_address }
define_user_address_tags("#{name_base}:work_address")
end
# Defines address tags
def define_user_address_tags(name_base,options={})
local=options[:local]
if !local
name_parts = name_base.split(":")
local = name_parts[-1]
end
define_value_tag("#{name_base}:display") { |t| t.locals.send(local).display(t.attr['separator'] || "<br/>") }
%w(address address_2 company phone fax city state zip country).each do |fld|
define_h_tag("#{name_base}:#{fld}") { |t| t.locals.send(local).send(fld) }
end
end
def image_tag_helper(tag,img,tag_opts) #:nodoc:
# Handle rollovers
if img.is_a?(Array)
rollover = img[1]
object_id = img[2]
img = img[0]
end
attr = tag.attr.clone
if img
icon_size = attr.delete('size') || tag_opts[:size] || nil
size = icon_size #%w(icon thumb preview small original).include?(icon_size) ? icon_size : nil
size = nil if size == 'original'
img_size = img.image_size(size) || []
shadow = attr.delete('shadow')
align = attr.delete('align')
field = attr.delete('field')
case field
when 'width':
img_tag = img_size[0].to_s
when 'height':
img_tag = img_size[1].to_s
when 'dimensions':
img_tag = "width='#{img_size[0].to_s}' height='#{img_size[1].to_s}'"
when 'src':
img_tag = img.url(size)
else
if shadow
shadow_align = align
border_amt = (attr.delete('border') || 1).to_i
if(align == 'left')
border = "0 #{border_amt}px #{border_amt}px 0px"
elsif(align == 'right')
border = "0 0 #{border_amt}px #{border_amt}px"
else
border = 0
end
else
attr['align'] = align
border_amt = (attr.delete('border') || 1).to_i
attr['style'] = (!attr['style'].blank?) ? attr['style'] + ';' : ''
if(align == 'left')
attr['style'] += "margin: 0 #{border_amt}px #{border_amt}px 0px;"
elsif(align == 'right')
attr['style'] += "margin: 0 0 #{border_amt}px #{border_amt}px"
end
end
img_url = img.url(size)
img_opts = { :src => img_url,:width => img_size[0],:height => img_size[1] }
attr.symbolize_keys!
if attr[:width] || attr[:height]
img_opts.delete(:width)
img_opts.delete(:height)
end
unless img_size[0] && img_size[0].to_i > 1 && img_size[1].to_i > 1
img_opts.delete(:width)
img_opts.delete(:height)
end
tag_opts = attr.merge(img_opts)
if rollover
tag_opts[:onmouseover] = "WebivaMenu.swapImage(this,'#{jvh img_url}','#{jvh rollover.url(size)}','#{object_id}'); " + tag_opts[:onmouseover].to_s
tag_opts[:onmouseout] = "WebivaMenu.restoreImage(this,'#{object_id}'); " + tag_opts[:onmouseout].to_s
preload = "<script>WebivaMenu.preloadImage('#{jvh rollover.url(size)}');</script>"
end
img_tag = tag('img',tag_opts) + preload.to_s
img_tag = "<div style='float:#{shadow_align}; margin:#{border}; #{attr['style']}'><div style='width:#{img_size[0] + 12}px; float:#{shadow_align};' class='cms_gallery_shadow'><div><p>" + img_tag + "</p></div></div></div>" if shadow
end
if tag.single?
img_tag
else
tag.locals.value = img_tag
tag.expand
end
else
nil
end
end
# Defines an image tag that expects a DomainFile to be returned from
# the yielded block and will create an html img tag
def define_image_tag(tag_name,local_obj=nil,attribute=nil,tag_opts={})
define_tag tag_name + ":value" do |tag|
tag.locals.value
end
obj_value = Proc.new() do |tag|
if local_obj
obj = tag.locals.send(local_obj)
if(obj)
if block_given?
if attribute
img = yield obj.send(attribute), tag
else
img = yield obj,tag
end
elsif attribute
img = obj.send(attribute)
else
img = obj
end
end
else
img = yield tag
end
end
tag_parts = tag_name.split(":")
tag_parts[-1] = "no_" + tag_parts[-1]
define_tag tag_parts.join(":") do |tag|
img = obj_value.call(tag)
img ? nil : tag.expand
end
define_tag tag_name do |tag|
img = obj_value.call(tag)
image_tag_helper(tag,img,tag_opts)
end
end
def define_form_tag(frm,name) #:nodoc:
define_tag name do |tag|
expand = block_given? ? yield : true
if expand
tag.locals.form = frm
"<form action='' method='post'><CMS:AUTHENTICITY_TOKEN/>" + tag.expand + "</form>"
else
nil
end
end
end
def define_ajax_form_for_tag(name,arg,options = {},&block)
options[:html] ||= {}
options[:html][:onsubmit] ||= ''
options[:html][:onsubmit] += " new Ajax.Updater('cmspara_#{paragraph.id}','#{ajax_url}',{ evalScripts: true, parameters: Form.serialize(this) }); return false;"
define_form_for_tag(name,arg,options,&block)
require_js('prototype')
end
# Creates a cms_unstyled_form_for object that can be used
# in combination with define_field_tag to allows users to
# custom style a form
def define_form_for_tag(name,arg,options = {})
frm_obj = options[:local] || 'form'
define_tag name do |tag|
obj = yield tag if block_given?
if obj || !block_given?
opts = options.clone
opts.symbolize_keys!
if obj.is_a?(Hash)
arg_obj = obj.delete(:object)
opts = opts.merge(obj)
obj = arg_obj
opts.symbolize_keys!
end
opts[:url] ||= ''
frm_opts = opts.delete(:html) || { }
frm_opts[:method] = 'post'
html_options = html_options_for_form(options.delete(:url),frm_opts)
html_options['action'] ||= ''
if pch = opts.delete(:page_connection_hash)
pch = "<input type='hidden' name='page_connection_hash' value='#{pch}' />"
end
frm_tag = tag(:form,html_options,true) + "<CMS:AUTHENTICITY_TOKEN/>" + pch.to_s + opts.delete(:code).to_s
cms_unstyled_fields_for(arg,obj,opts) do |f|
tag.locals.send("#{frm_obj}=",f)
frm_tag + tag.expand + "</form>"
end
else
nil
end
end
end
def define_fields_for_tag(name,arg,options = {}) #:nodoc:
frm_obj = options.delete(:local) || 'form'
define_tag name do |tag|
obj = yield tag
opts = options.clone
opts[:url] ||= ''
if obj || !block_given?
cms_unstyled_fields_for(arg,obj,opts) do |f|
tag.locals.send("#{frm_obj}=",f)
opts.delete(:code).to_s + tag.expand
end
else
nil
end
end
end
def define_publication_form_error_tag(name,publication,options={})#:nodoc:
frm_obj = options.delete(:local) || 'form'
define_value_tag name do |t|
output = []
frm = t.locals.send(frm_obj)
publication.content_publication_fields.each do |fld|
err = frm.output_error_message(fld.label,fld.content_model_field.field)
output << err if err
end
output.join()
end
end
# Allows users to display the full error messages of a form_for_tag
def define_form_error_tag(name,options={})
frm_obj = options.delete(:local) || 'form'
name_parts = name.split(":")
name_base = name_parts[-1]
define_tag "#{name_parts[0..-2].join(":")}:no_#{name_base}" do |t|
frm = t.locals.send(frm_obj)
if frm && frm.object && frm.object.errors && frm.object.errors.length > 0
nil
else
t.expand
end
end
define_tag name do |tag|
if block_given?
objs = yield tag
objs = [ objs ] unless objs.is_a?(Array)
else
frm = tag.locals.send(frm_obj)
objs = [ frm.object ] if frm && frm.object
end
objs ||= []
objs = objs.compact
error_count = objs.inject(0) { |acc,obj| acc + obj.errors.length }
if error_count > 0
messages = objs.inject([]) { |acc,obj| acc + obj.errors.full_messages }.join(tag.attr['separator'] || "<br/>")
if tag.single?
messages
else
tag.locals.value = messages
tag.expand
end
else
nil
end
end
define_tag "#{name}:value" do |tag|
tag.locals.value
end
end
# Defines a submit button tag
def define_button_tag(name,options={})
onclick = options.delete(:onclick)
define_tag name do |t|
name = options[:name] || t.attr['name'] || 'commit'
if !block_given? || yield(t)
if(t.attr['type'].to_s == 'image')
tag('input',
:type => 'image',
:class => 'submit_tag submit_image',
:name => name,
:value => t.attr['value'] || options[:value] || 'Submit',
:src => t.expand,
:id => t.attr['id'] || options[:id],
:align => :absmiddle,
:onclick => onclick )
else
tag('input', { :type => 'submit',
:class => 'submit_tag',
:name => name,
:id => t.attr['id'] || options[:id],
:value => t.single? ?
(t.attr['value'] || options[:value] || 'Submit') : t.expand,
:onclick => onclick })
end
else
nil
end
end
end
def define_delete_button_tag(name,options={}) #:nodoc:
frm_obj = options[:local] || 'form'
define_tag name do |t|
if t.locals.send(frm_obj).object.id.blank?
nil
else
<<-TAGS
<input type='hidden' name='#{t.locals.send(frm_obj).object_name}_delete' value='0' id='#{t.locals.send(frm_obj).object_name}_delete' />
<input type='submit' onclick='$("#{t.locals.send(frm_obj).object_name}_delete").value="1"; return true;' value='#{vh t.expand}'/>
TAGS
end
end
end
def define_submit_tag(tag_name,options = {:default => 'Submit'.t }) #:nodoc:
self.define_tag tag_name do |tag|
output =''
if tag.single?
txt = tag.attr['value'] || options[:default]
output ="<input type='submit' name='button' value='#{vh txt}'/>"
else
if !block_given? || yield
if tag.attr['type'] == 'image'
output = "<input type='image' src='#{vh tag.expand}' align='absmiddle'/>"
else
output = "<input type='submit' class='submit_tag' name='button' value=\"#{tag.expand}\"/>"
end
else
output = nil
end
end
if(options.has_key?(:form))
if !options[:form]
nil
else
"<form action='#{options[:form]}' method='#{options[:method]||'get'}'>" + output + "</form>"
end
else
output
end
end
end
# Defines a link tag that expects either a string representing the
# href of a link or a hash with additional tag attributes (like :noclick)
#
# For example:
#
# define_link_tag('result') { |t| '/result' }
#
# would define 3 tags:
# [<cms:result_link>]
# Block tag that creates s <a>..</a> tag wrapping it's content
# [<cms:result_url/>]
# Value tag that returns the url of the link tag
# [<cms:result_href/>]
# Value tag that returns a href="url.." tag
def define_link_tag(name,options={})
name_base = name[-1..-1] == ":" ? name : name + "_"
define_value_tag(name_base + "url") do |tg|
url = yield tg
if url.is_a?(Hash)
url[:href]
else
url
end
end
define_tag(name_base + "link") do |tg|
attr = tg.attr.clone
selected = attr.delete('selected_class')
url = yield(tg)
if url.blank?
nil
elsif url.is_a?(Hash)
url_selected = url.delete(:selected)
url[:class] = selected if selected && url_selected
tag('a',attr.merge(options).merge(url),true) + tg.expand + "</a>"
else
tag('a',attr.merge(options).merge({ :href => url }),true) + tg.expand + "</a>"
end
end
define_value_tag(name_base + "href") do |tg|
url = yield tg
if url.is_a?(Hash)
url.to_a.map { |elm| "#{elm[0]}='#{vh elm[1]}'" }.join(" ")
else
"href='#{url}'"
end