Posted By

flyingmachine on 04/07/09


Tagged

view widget merb


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


merb widget


 / Published in: Ruby
 

  1. # lib/widgets/widget.rb
  2. # Superclass for our form element "widgets"
  3. class Widget
  4. include Merb::GlobalHelpers
  5.  
  6. # This is necessary to include Merb::GlobalHelpers
  7. class_inheritable_accessor :_default_builder
  8.  
  9. # These are for convenience
  10. attr_accessor :params
  11. attr_accessor :session
  12.  
  13. def initialize(params = {}, session = {})
  14. self.params = params
  15. self.session = session
  16. end
  17.  
  18. def value
  19. end
  20. end
  21.  
  22. # lib/widgets/weight_input_switcher.rb
  23. class WeightInputSwitcher < Widget
  24. attr_accessor :attribute_name, :weight_in_kg, :unit_preference
  25.  
  26. # View methods
  27. def setup(attribute_name, weight_in_kg = nil, unit_preference = session[:unit_preference])
  28. self.attribute_name = attribute_name
  29. self.weight_in_kg = weight_in_kg
  30. self.unit_preference = unit_preference
  31. self
  32. end
  33.  
  34. def switcher
  35. html = "<div class='weight_input_switcher'>"
  36. html += weight_input("lbs")
  37. html += weight_input("kg")
  38. html += weight_input("st")
  39. html += "</div>"
  40. html
  41. end
  42.  
  43. def weight_input
  44. html = "<span class='weight_input #{unit_preference}'>"
  45. html += weight_input_field
  46. html += "</span>"
  47. end
  48.  
  49. def weight_input_field
  50. if unit_preference == "st"
  51. stone, lbs = if weight_in_kg.to_i == 0
  52. ["",""]
  53. else
  54. weight_input_value(weight_in_kg, unit_preference).split(" st ")
  55. end
  56.  
  57. html = text_field attribute_name, :value => stone, :name => "weight_input[stone]", :class => "weight_input_field stone"
  58. html += "<span class='weight_unit'>stone</span>"
  59. html += text_field attribute_name, :value => lbs, :name => "weight_input[lbs]", :class => "weight_input_field stone lbs"
  60. html += "<span class='weight_unit'>lbs</span>"
  61. else
  62. value = weight_input_value(weight_in_kg, unit_preference)
  63.  
  64. html = text_field attribute_name, :value => value, :name => "weight_input", :class => "weight_input_field"
  65. html += "<span class='weight_unit'>" + unit_preference.to_s + "</span>"
  66. end
  67. html
  68. end
  69.  
  70. # Parse methods
  71. # We kinda cheat here and use params. Need to figure out a better way to do this.
  72. def value
  73. if params["weight_input"]["stone"]
  74. "#{params["weight_input"]["stone"]} st #{params["weight_input"]["lbs"]} lbs"
  75. else
  76. "#{params["weight_input"]} #{Units.weight_full_to_abbreviation_map[unit_preference]}"
  77. end
  78. end
  79.  
  80. private
  81. def weight_input_value(weight_in_kg, unit_preference)
  82. begin
  83. weight_in_kg.to_display_weight_without_unit(unit_preference)
  84. rescue
  85. ""
  86. end
  87. end
  88. end
  89.  
  90. # The following is added to global_helpers.rb
  91. def widget(klass, *args)
  92. klass.new(params, session).setup(*args)
  93. end
  94.  
  95. # An example of using the "Widget" to display form elements
  96. widget(WeightInputSwitcher, :weight, @current_user.current_weight).switcher
  97.  
  98. # An example of using the "Widget" to get the value of form elements
  99. weight_widget = widget(WeightInputSwitcher, :weight, 0, "", user[:unit_preference])
  100. weight = weight_widget.value

Report this snippet  

You need to login to post a comment.