Module: Lolita::Controllers::ComponentHelpers
- Defined in:
- lib/lolita/controllers/component_helpers.rb
Overview
controller instance when component is rendered. All components ar placed in “app/helpers/components/[your component path]”. Component should have fallowing module structure Components::::[Component name]Component
Components::Lolita::ListComponent
Example
render_component :"lolita/configuration/list", :dispaly
# try to find /helpers/components/lolita/list_component.rb in every directory in $: that
# ends with /helpers
# require this file if found and extend self with Components::Lolita::ListComponent.
Component helpers is loaded in same order as views or controller.
Instance Method Summary collapse
-
#component_helper_path(component_name) ⇒ Object
Find path for given component.
- #helpers_for_component(component_name) ⇒ Object
-
#render_component(*args) ⇒ Object
Render partial template.
-
#will_use_component(component_name) ⇒ Object
Require component helper file and extend current instance with component helper module.
- #with_format(format, &block) ⇒ Object
Instance Method Details
#component_helper_path(component_name) ⇒ Object
Find path for given component.
component_helper_path :"lolita/list" #=> [path_to_lolita]/app/helpers/components/lolita/list_component.rb
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lolita/controllers/component_helpers.rb', line 84 def component_helper_path component_name @helper_paths||=$:.reject{|p| !p.match(/\/helpers$/)} get_path=lambda{|paths| extra_path=component_name.to_s.split("/") component=extra_path.pop paths.each do |path| new_path=File.join(path,"components",*extra_path,"#{component}_component.rb") if File.exist?(new_path) return new_path end end nil } path=get_path.call(@helper_paths) path end |
#helpers_for_component(component_name) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/lolita/controllers/component_helpers.rb', line 73 def helpers_for_component component_name names=component_name.to_s.split("/") start_index=1 # first is lolita start_index.upto(names.size) do |index| yield names.slice(0..index).join("/") end end |
#render_component(*args) ⇒ Object
Render partial template. Accept: name - name for component in ‘/components’ directory,
can be full name too.
Example 'lolita/list'
state - name for partial. Example ‘row’. options - any options to pass as :locals to partial,
also available through <code>@opts</code> variable.
Example
render_component "lolita/list", :display
render_component "lolita/list/display"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lolita/controllers/component_helpers.rb', line 28 def render_component *args @opts=args. name=args[0] state=args[1] format=@opts.delete(:format) raise "Can't render component without name!" unless name will_use_component name partial_name=File.join("/components",name.to_s,state ? state.to_s : nil) if format with_format(format) do render(:partial=>partial_name,:locals=>@opts) end else render(:partial=>partial_name,:locals=>@opts) end end |
#will_use_component(component_name) ⇒ Object
Require component helper file and extend current instance with component helper module.
Example
will_use_component :"lolita/configuration/list"
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lolita/controllers/component_helpers.rb', line 57 def will_use_component component_name helpers_for_component(component_name) do |possible_component_name| @used_component_helpers||=[] unless @used_component_helpers.include?(possible_component_name) if path=component_helper_path(possible_component_name) self.class.class_eval do require path end class_name=possible_component_name.to_s.camelize self.extend("Components::#{class_name}Component".constantize) rescue nil #FIXME too slow end @used_component_helpers<<possible_component_name end end end |
#with_format(format, &block) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/lolita/controllers/component_helpers.rb', line 46 def with_format(format, &block) old_formats = formats self.formats = [format] result=block.call self.formats = old_formats result end |