| Class | ApplicationController |
| In: |
app/controllers/application.rb
|
| Parent: | ActionController::Base |
Filters added to this controller apply to all controllers in the application. Likewise, all the methods added will be available for all controllers.
| MaxRows | = | 6 | See ActionController::Base for details Uncomment this to filter the contents of submitted sensitive data parameters from your application log (in this case, all fields with names like "password"). filter_parameter_logging :password | |
| WordsDir | = | "words" | ||
| MaxWords | = | 30 | ||
| SentencesDir | = | 'sentences' | ||
| LineLen | = | 150 |
# File app/controllers/application.rb, line 461
461: def backspace_icon
462: # "<"
463: ??{ <IMG height=60 src= "/images/no.jpg" alt ="something
464: else" width=60 /> }
465: end
session garbage collection
# File app/controllers/application.rb, line 492
492: def clear_unused_in_session
493: session[:choices]=session[:start_choices]=nil
494: session[:user_choice]=nil
495: session[:computer_choice]=nil
496: session[:sticks]=nil
497: session[:displayed]=nil
498: session[:picked]=nil
499: session[:toggle]=nil
500: session[:display]=nil
501: session[:pictures]=nil
502: session[:wrong]=nil
503: session[:freeze]=nil
504: session[:answer]=nil
505: session[:state]=nil
506: session[:correct]=nil
507: session[:picfolder]=nil
508: session[:separator]=nil
509: # session[:nitems]=nil
510: end
# File app/controllers/application.rb, line 451
451: def down_icon
452: # "v"
453: ??{<IMG height=60 src="/images/down.gif" alt ="down"
454: width=60 />}
455: end
filechoices? is true for static data, the kind that can be stored in files and locally modified
# File app/controllers/application.rb, line 311
311: def filechoices?
312: true
313: end
first_time will often be overridden
# File app/controllers/application.rb, line 653
653: def first_time
654: redirect_to :action => "index"
655: end
~
# File app/controllers/application.rb, line 127
127: def format_pic (pic)
128: '<IMG height=150 src= "/'+pic + '" width=200 /> '
129: end
get_and_reset_typed helps modules contribute text via session[:typed] which this function resets
# File app/controllers/application.rb, line 258
258: def get_and_reset_typed
259: t = session[:typed]
260: session[:typed]=nil
261: t
262: end
# File app/controllers/application.rb, line 298
298: def get_bgcolor
299: session[:bgcolor] ||= 'black'
300: end
# File app/controllers/application.rb, line 511
511: def get_buttons
512: {"up" => up_icon, "down" => down_icon,
513: "backspace" => backspace_icon,
514: "select" => select_icon}
515: end
get_choices is a getter for session[:choices] if that is nil it consults filechoices to either pick up choices from the appropriate file or invokes start_choices
# File app/controllers/application.rb, line 318
318: def get_choices
319: session[:choices] ||
320: if filechoices?
321: if @thiscontroller
322: fd=MenuFolder.chop
323: Dir.mkdir(fd) unless File.directory?(fd)
324: fch=MenuFolder+@thiscontroller+'choices.txt'
325: if File.exist?(fch)
326: #logger.info "reading #{fch}"
327: chs= IO.readlines(fch)
328: chs.collect!{|s|s.strip}
329: else
330: chs=start_choices
331: File.open(fch,"w") do |f|
332: f.puts chs
333: end
334: end
335: chs
336: else
337: start_choices
338: end
339: else
340: start_choices
341: end
342: end
get_choices_size is used to
normalize selected_item,
so 0 is not ok
# File app/controllers/application.rb, line 386
386: def get_choices_size
387: @choices = get_choices
388: @choices.size ==0? 1: @choices.size
389: end
# File app/controllers/application.rb, line 516
516: def get_delay
517: session[:delay] ||= 5
518: end
# File app/controllers/application.rb, line 444
444: def get_displayed
445: session[:displayed] ||= ""
446: end
get_folder returns i‘th folder
# File app/controllers/application.rb, line 60
60: def get_folder(i)
61: fl=get_folder_list
62: fl.empty? ? '': fl[i% fl.size]
63: end
current folder for picbrowser, recognize,..
# File app/controllers/application.rb, line 56
56: def get_folder_index
57: session[:folder_index] ||= 0
58: end
get_folder_list returns array of image folders
# File app/controllers/application.rb, line 49
49: def get_folder_list
50: g=Giver.new
51: folders=g.get_subdirs("images")
52: logger.info "image folders #{folders}"
53: folders
54: end
one choice is shown in selected_font get_highlighted_choice returns it
# File app/controllers/application.rb, line 148
148: def get_highlighted_choice
149: if @choices.empty?
150: ''
151: else
152: @choices[@selected_item% get_choices_size]
153: end
154: end
get_modules returns corr session ensuring an item in it is ‘modules‘
# File app/controllers/application.rb, line 265
265: def get_modules
266: session[:modules] ||= []
267: (session[:modules] << "modules").uniq!
268: session[:modules]
269: end
# File app/controllers/application.rb, line 343
343: def get_overflow
344: session[:overflow] ||=''
345: end
# File app/controllers/application.rb, line 660
660: def get_para(lw, item_no)
661: displayed=''
662: if !lw.empty?
663: begin
664: page=Wikipedia.new.page(lw)
665: logger.info("wikipage for #{lw}:")
666: logger.info(page)
667: doc =Hpricot(page)
668: bc=doc.search('#bodyContent')
669: ps = bc/:p
670: rescue Exception
671: logger.info "pageerror #{$!}"
672: ps=[]
673: @title='no wikipedia access'
674: end
675: if ps.empty?
676: displayed='nothing found'
677: elsif ps.size==1
678: paras=[ps.html]
679: lis=bc/:li
680: lis.each { |i| paras << i.html}
681: item_no%=paras.size
682: displayed = paras[item_no]
683: else
684: ps.reject! {|pa| pa.inner_text !~ /\w/}
685: # @paras.delete_at(0)
686: # logger.info('wikipedia paras'+@paras.inspect)
687: item_no%=ps.size
688: displayed = ps[item_no].inner_html
689: end
690: displayed.gsub!('/wiki/','/wikipedia/clicked/')
691: end
692: displayed
693: end
get_pic returns normalized i‘th picture from desired folder
# File app/controllers/application.rb, line 81
81: def get_pic(folder, i)
82: g ||= Giver.new
83: rx=Regexp.new(
84: 'images/(.*(JPG|GIF|PNG))$',
85: Regexp::IGNORECASE)
86: pictures=g.get_filename_part('images/'+ folder ,rx)
87: pic= pictures[i% pictures.size] || ''
88: # logger.info("giving picture #{i} from "+folder+':
89: #' +pic
90: #)
91: pic
92: end
get_pic_file returns a filename suitable for
passing to image_tag if what is in name
is the basename of an image file in the
subfolders of public/images
# File app/controllers/application.rb, line 100
100: def get_pic_file(name)
101: g=Giver.new
102: pf=g.get_filename_part(pic_folder,
103: Regexp.new('images/(.*/'+name+'\.(JPG|GIF|PNG))$',
104: Regexp::IGNORECASE))
105: if pf.empty?
106: ''
107: else
108: pf.first
109: end
110: end
get_pic_from_folder returns filename suitable for
passing to image_tag if what is in name
is the basename of an image file in the
subfolders of public/images/folder
# File app/controllers/application.rb, line 115
115: def get_pic_from_folder(name,folder)
116: g=Giver.new
117: pf=g.get_filename_part("images/#{folder}",
118: Regexp.new('images/(.*/'+name+'\.(JPG|GIF|PNG))$',
119: Regexp::IGNORECASE))
120: if pf.empty?
121: ''
122: else
123: pf.first
124: end
125: end
get_pic_html returns the html for picname if such a pic exists, else empty string
# File app/controllers/application.rb, line 132
132: def get_pic_html(picname)
133: p=get_pic_file picname
134: if p.empty?
135: p
136: else
137: format_pic p
138: end
139: end
# File app/controllers/application.rb, line 477
477: def get_postselected
478: session[:postselected] ||='</SPAN></FONT></FONT></P>'
479: end
# File app/controllers/application.rb, line 480
480: def get_preselected
481: session[:preselected] ||=
482: '<P ALIGN=LEFT><FONT COLOR="black"><FONT SIZE="7"
483: STYLE="font-size:15pt"><SPAN STYLE=
484: "background: skyblue;font-family: Times New Roman">'
485: end
# File app/controllers/application.rb, line 486
486: def get_preunselected
487: session[:preunselected] ||=
488: '<P ALIGN=LEFT><FONT COLOR="blue"><FONT SIZE="7"
489: STYLE="font-size: 15pt; font-family: Arial"><B>'
490: end
get_rxpics returns an array of pictures whose names match rx
# File app/controllers/application.rb, line 142
142: def get_rxpics(folder, rx)
143: g ||= Giver.new
144: pictures=g.get_filename_part('images/'+ folder ,rx)
145: end
# File app/controllers/application.rb, line 278
278: def get_selected_color
279: session[:selected_color] ||= 'yellow'
280: end
# File app/controllers/application.rb, line 282
282: def get_selected_font
283: session[:selected_font] ||= 'Times New Roman'
284: end
# File app/controllers/application.rb, line 270
270: def get_selected_item
271: session[:selected_item] ||=0
272: end
# File app/controllers/application.rb, line 274
274: def get_selected_size
275: session[:selected_size] ||= '12pt'
276: end
separator used to fit more than one choice per line if the separator is nowhere among the choices, reformat_choices tries to rearrange the choices using the separator evenly among the rows
# File app/controllers/application.rb, line 35
35: def get_separator
36: ','
37: end
stepsize is used in picture editing
# File app/controllers/application.rb, line 45
45: def get_stepsize
46: session[:stepsize] ||= 10
47: end
get_text is a getter that also processes any contributions from other modules via session[:typed]
# File app/controllers/application.rb, line 357
357: def get_text
358: session[:text] ||= "welcome to skid"
359: g=get_and_reset_typed
360: if g
361: session[:text]+=" #{g} "
362: end
363: session[:text]
364: end
# File app/controllers/application.rb, line 467
467: def get_title
468: session[:title] ||= "Welcome to skid"
469: end
# File app/controllers/application.rb, line 290
290: def get_unselected_color
291: session[:unselected_color] ||= 'blue'
292: end
# File app/controllers/application.rb, line 294
294: def get_unselected_font
295: session[:unselected_font] ||= 'Arial'
296: end
# File app/controllers/application.rb, line 286
286: def get_unselected_size
287: session[:unselected_size] ||= '12pt'
288: end
index is where everything must end for it is the only view
# File app/controllers/application.rb, line 521
521: def index
522: # logger.info "myid #{params[:id]}"
523: @settings={}
524: if params[:id]=~ /_refresh/
525: session[:selected_item] ||= 0
526: session[:selected_item]+=1
527: session[:auto_refresh]=true
528: updownevent
529: end
530: if params[:id]=~ /stopscroll/
531: session[:auto_refresh]=false
532: end
533: if params[:id]=~ /nobuttons/
534: session[:nobuttons]=true
535: @settings['with buttons']='buttons'
536: end
537: if params[:id]=~ /withbuttons/
538: session[:nobuttons]=nil
539: end
540: if params[:id]=~ /nomodules/
541: session[:nomodules]=true
542: end
543: if params[:id]=~ /withmodules/
544: session[:nomodules]=nil
545: end
546: if params[:id]=~ /delay/
547: delay=params[:id].sub('delay','').to_i
548: if delay > 0
549: session[:delay]=delay
550: end
551: end
552: if params[:id]=~ /nopics/
553: session[:nopics]=true
554: end
555: if params[:id]=~ /withpics/
556: session[:nopics]=nil
557: end
558: if params[:id]=~ /nowords/
559: session[:nowords]=true
560: end
561: if params[:id]=~ /withwords/
562: session[:nowords]=nil
563: end
564: @refresh= session[:auto_refresh]
565: @delay=get_delay
566: @with_words = !session[:nowords]
567: if session[:auto_refresh]
568: @settings['manual scrolling']='stopscroll'
569: else
570: @settings['automatic scrolling']='auto_refresh'
571: end
572: if session[:nobuttons]
573: @settings['with buttons']='withbuttons'
574: else
575: @settings['without buttons']='nobuttons'
576: end
577: if session[:nomodules]
578: @settings['with modules']='withmodules'
579: else
580: @settings['without modules']='nomodules'
581: end
582: if session[:nopics]
583: @settings['with pictures in choices']='withpics'
584: else
585: @settings['without pictures in choices']='nopics'
586: end
587: if session[:nowords]
588: @settings['with words in choices']='withwords'
589: else
590: @settings['without words in choices']='nowords'
591: end
592: myname=self.class.name
593: if myname!=session[:current_controller]
594: session[:current_controller]=myname
595: clear_unused_in_session
596: first_time
597: end
598: @thiscontroller=myname.
599: gsub(/Controller/,'').downcase
600: if session[:nomodules]
601: @modules=[]
602: else
603: @modules=get_modules
604: #used for the module table on top
605: end
606: @title=get_title #the title top right
607: @preselected=get_preselected #for font
608: @postselected=get_postselected #see application_helper.rb
609: @preunselected= get_preunselected #and the font module
610: @postunselected=get_postunselected
611: #for these 4 variables
612: if session[:nobuttons]
613: @buttons={}
614: else
615: @buttons=get_buttons
616: end
617: @choices=get_choices
618: @selected_item=get_selected_item
619: if !@choices.empty?
620: reformat_choices
621: @selected_item%=@choices.size
622: # if @selected_item>0
623: # @choices[0..@selected_item-1]=[]
624: # @selected_item=0
625: # end
626: @color= get_unselected_color
627: @selcolor=get_selected_color
628: end
629: extras
630: @spoken= session[:spoken]
631: session[:spoken]=nil
632: @choices.each_index do |i|
633: @choices[i]=make_clickable(@choices[i],i)
634: end
635: @pictures=@choices.collect do|cho|
636: get_pic_file(strip_href(cho))
637: end unless session[:nopics]
638: @extracols=@modules.size
639: if session[:nopics] then @extracols-=1 end
640: if @extracols< 1 then @extracols=1 end
641: @designers=designers
642: # logger.info "session contents #{session.inspect}"
643: # logger.info "@modules #{@modules.inspect} session
644: #{session[:nomodules].inspect}"
645: end
gets the last word from str
# File app/controllers/application.rb, line 232
232: def last_word(str)
233: if str && str.strip.match('(\w+)$')
234: $1
235: else
236: ''
237: end
238: end
make_clickable returns unchanged anything containing an anchor. otherwise encloses tch in an anchor link. Displayed is num if it exists else tch with space replaced by - unless nowords set
# File app/controllers/application.rb, line 164
164: def make_clickable(tch, num=nil)
165: if tch.match(/<a/)
166: tch
167: else
168: sch=show=''
169: if num
170: sch='_'+num.to_s
171: elsif !tch.match(/<\W*img/)
172: sch=tch.gsub(/ /,'-')
173: end
174: show='<a href="/'+selfname+'/select/'+sch+'">' +
175: tch +'</a>'
176: end
177: end
max_choices_in_line is used in an attempt to evenly divide the choices among several lines
# File app/controllers/application.rb, line 180
180: def max_choices_in_line
181: Math.sqrt(@choices.size).floor+1
182: end
process_choice decides what to do when a choice
is made: splits it if it contains a separator else calls selected
# File app/controllers/application.rb, line 399
399: def process_choice selected_choice
400: logger.info "selected_choice#{selected_choice}" +
401: "get_separator #{get_separator}"
402: if (selected_choice.include?(get_separator) and
403: (selected_choice.length>1))
404:
405: session[:choices]=selected_choice.
406: split(get_separator)
407: # logger.info "separated #{session[:choices].inspect}"
408: else
409: str=strip_href(selected_choice)
410: #logger.info("selected: #{str}")
411: selected (str)
412: session[:choices]=nil
413: session[:selected_item]=0
414: end
415:
416: end
random_pics returns n picture names at random from the desired folder
# File app/controllers/application.rb, line 66
66: def random_pics(folder, n)
67: g ||= Giver.new
68: rx=Regexp.new('images/'+folder+
69: '/(.*)\.(JPG|GIF|PNG)$',
70: Regexp::IGNORECASE)
71: words=g.get_filename_part(
72: 'images/'+ folder ,rx)
73: selwords=[]
74: while (selwords.size < n) && !words.empty?
75: selwords.push words.delete_at(rand(words.size))
76: end
77: selwords
78: end
reformat_choices reformats @choices and makes each into a link
# File app/controllers/application.rb, line 191
191: def reformat_choices
192: if # !@choices or
193: (@choices.size<=MaxRows) or
194: (@choices.join.match(get_separator))
195: @choices.collect! do |cl|
196: ca=cl.split(get_separator)
197: ca.collect!{|c| make_clickable c}
198: s=ca.join(get_separator)
199: # logger.info "reformatted choices #{ca}"
200: s
201: end
202: return
203: end
204: @lines=[]
205: lcount=0
206: # session[:selected_item]=@selected_item=0
207: #max_choices_in_line=Math.sqrt(@choices.size).floor+1
208: @choices=@choices.reverse
209: (0..MaxRows).each do |row|
210: @lines[row]=''
211: (0..max_choices_in_line).each do |item|
212: tch=@choices.pop
213: @lines[row] << make_clickable(tch) <<
214: get_separator
215: # if (@lines[row].length > LineLen) or
216: if line_full(@lines[row]) or
217: @choices.empty?
218: break
219: end
220: end
221: if @choices.empty?
222: break
223: end
224: end
225: @choices=
226: @lines.collect do |l|
227: l.strip.chop
228: # removes separator at the end
229: end
230: end
select is activated when the smiley is clicked if the id is a number, that choice is taken if the id is a string, it is selected
# File app/controllers/application.rb, line 420
420: def select
421: if params[:id]
422: if params[:id]=~/_(\d+)/
423: i=$1.to_i
424: session[:selected_item]=i
425: # process_choice(get_choices[i])
426: # logger.info "sid: #{params[:id]}"
427: else
428: selected(params[:id].gsub(/-/,' '))
429: what_next
430: return
431: end
432: end
433: @choices=get_choices
434: reformat_choices
435: @selected_item= get_selected_item
436: # logger.info "#{@selected_item} from choices #{
437: # @choices.inspect}"
438: if !@choices.empty?
439: selected_choice=get_highlighted_choice
440: process_choice selected_choice
441: end
442: what_next
443: end
# File app/controllers/application.rb, line 456
456: def select_icon
457: # ">"
458: ??{ <IMG height=60 src= "/images/yes.jpeg" alt ="select"
459: width=60 /> }
460: end
def selector
'|'
end
start_choices are defined differently by each module
# File app/controllers/application.rb, line 306
306: def start_choices
307: %w{should be overridden}
308: end
strip_href returns the displayed portion of a link
# File app/controllers/application.rb, line 392
392: def strip_href(str)
393: str.match(%r{>(.+)</a>}) ? $1.strip : str
394: end
# File app/controllers/application.rb, line 447
447: def up_icon
448: ??{ <IMG height=60 src= "/images/up.gif" alt ="up"
449: width=60 /> }
450: end
updownevent is overridden by modules that need some processing each time up or down is pressed
# File app/controllers/application.rb, line 241
241: def updownevent
242: end
what_next enables you, after selection, to go someplace else
by setting session[:controller]
# File app/controllers/application.rb, line 368
368: def what_next
369: if session[:controller]
370: s=session[:controller]
371: session[:controller]=nil
372: redirect_to( :action => "first_time",
373: :controller => s) and return
374: else
375: redirect_to :action => "index"
376: end
377: end