lib/apotomo/widget_shortcuts.rb in apotomo-1.0.5 vs lib/apotomo/widget_shortcuts.rb in apotomo-1.1.0.rc1

- old
+ new

@@ -1,59 +1,33 @@ module Apotomo # Shortcut methods for creating widget trees. module WidgetShortcuts - # Shortcut for creating an instance of +class_name+ named +id+. - # If +start_state+ is omited, :display is default. Yields self. + # Shortcut for creating an instance of <tt>class_name+"_widget"</tt> named +id+. Yields self. # # Example: # - # widget(:comments_widget, 'post-comments') - # widget(:comments_widget, 'post-comments', :user => @current_user) + # widget(:comments) + # + # will create a +CommentsWidget+ with id :comments. # - # Start state is <tt>:display</tt>, whereas the latter also populates @opts. + # widget(:comments, 'post-comments', :user => current_user) # - # widget(:comments_widget, 'post-comments', :reload) - # widget(:comments_widget, 'post-comments', :reload, :user => @current_user) + # sets id to 'posts_comments' and #options to the hash. # - # Explicitely sets the start state. - # # You can also use namespaces. # # widget('jquery/tabs', 'panel') - def widget(class_name, id, state=:display, *args) - if state.kind_of?(Hash) - args << state - state = :display - end + def widget(prefix, *args) + options = args.extract_options! + id = args.shift || prefix - object = constant_for(class_name).new(parent_controller, id, state, *args) - yield object if block_given? - object + constant_for(prefix).new(parent_controller, id, options).tap do |object| + yield object if block_given? + end end - def container(id, *args, &block) - widget('apotomo/container_widget', id, *args, &block) - end - - def section(*args) - container(*args) - end - - # TODO: deprecate. - def cell(base_name, states, id, *args) - widget(base_name.to_s + '_cell', states, id, *args) - end - - def tab_panel(id, *args) - widget('apotomo/tab_panel_widget', :display, id, *args) - end - - def tab(id, *args) - widget('apotomo/tab_widget', :display, id, *args) - end - private - def constant_for(class_name) - class_name.to_s.camelize.constantize + def constant_for(class_name) # TODO: use Cell.class_from_cell_name. + "#{class_name}_widget".classify.constantize end end end