Module: Guilded::Rails::ViewHelpers

Defined in:
lib/guilded/rails/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#g_apply_behaviorObject

Generates the JavaScript includes for each Guilded element that is used. Also generates the initGuildedElements function and includes a call to each GUIlded elements Init method.

Must be called once per rendered page. You can include it just before the closing body tag of your application layout. If no Guilded elements were called in the template, the call to g_apply_behavior will not output anything.



13
14
15
16
17
18
# File 'lib/guilded/rails/view_helpers.rb', line 13

def g_apply_behavior
  self.output_buffer.sub!( /<!-- guilded.styles -->/, stylesheet_link_tag( Guilded::Guilder.instance.combined_css_srcs, :cache => "cache/#{Guilded::Guilder.instance.css_cache_name}" ) )
  html = javascript_include_tag( Guilded::Guilder.instance.combined_js_srcs, :cache => "cache/#{Guilded::Guilder.instance.js_cache_name}" )
  html << Guilded::Guilder.instance.generate_javascript_init
  html
end

#g_apply_styleObject



20
21
22
# File 'lib/guilded/rails/view_helpers.rb', line 20

def g_apply_style
  "<!-- guilded.styles -->"
end

#g_browser_full_nameObject

Returns the browser name concatenated with the browser version. for example, ‘ie7’. When using the browser detector in the contoller you may put a :g_browser_detector variabe in the session if you wish to keep the BrowserDetector from being instantiated more than once per request.



125
126
127
128
129
# File 'lib/guilded/rails/view_helpers.rb', line 125

def g_browser_full_name
  @g_browser_detector = session[:g_browser_detector] if respond_to?( :session )
  @g_browser_detector ||= Guilded::BrowserDetector.new( request )
  @g_browser_detector.browser_full_name
end

#g_browser_is?(options = {}) ⇒ Boolean

Returns true if the browser matches the options ent in, otherwise returns false. When using the browser detector in the contoller you may put a :g_browser_detector variabe in the session if you wish to keep the BrowserDetector from being instantiated more than once per request.

Options

  • :name - The name of the browser. For example ‘ie’.

  • :version - The version of the browser. For example ‘7’.

Returns:

  • (Boolean)


139
140
141
142
143
# File 'lib/guilded/rails/view_helpers.rb', line 139

def g_browser_is?( options={} )
  @g_browser_detector = session[:g_browser_detector] if respond_to?( :session )
  @g_browser_detector ||= Guilded::BrowserDetector.new( request )
  @g_browser_detector.browser_is?( options )
end

#g_browser_nameObject

Returns the name of the browser that is making this request. For example ‘ie’. When using the browser detector in the contoller you may put a :g_browser_detector variabe in the session if you wish to keep the BrowserDetector from being instantiated more than once per request.



105
106
107
108
109
# File 'lib/guilded/rails/view_helpers.rb', line 105

def g_browser_name
  @g_browser_detector = session[:g_browser_detector] if respond_to?( :session )
  @g_browser_detector ||= Guilded::BrowserDetector.new( request )
  @g_browser_detector.browser_name
end

#g_browser_versionObject

Returns the version of the browser that is making this request. For example ‘7’. When using the browser detector in the contoller you may put a :g_browser_detector variabe in the session if you wish to keep the BrowserDetector from being instantiated more than once per request.



115
116
117
118
119
# File 'lib/guilded/rails/view_helpers.rb', line 115

def g_browser_version
  @g_browser_detector = session[:g_browser_detector] if respond_to?( :session )
  @g_browser_detector ||= Guilded::BrowserDetector.new( request )
  @g_browser_detector.browser_version
end

#g_javascript_include_tag(*sources) ⇒ Object

Creates a javascript include tag for a Guilded specific file. The only difference being that it adds the file to a sources array to be concatenated and included at the end of the page with the dependencies specified for the Guilded components used.

To explicitly include the jQuery or MooTools libraries you can use :jquery and/or :mootools respectively. If a component that uses either jQuery or MooTools is used on a page, there is no need to explicitly include the library, as it will be resolved as a dependency and only included once.



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
# File 'lib/guilded/rails/view_helpers.rb', line 42

def g_javascript_include_tag( *sources )
  g = Guilded::Guilder.instance
  defaults = nil
  if sources.include?( :defaults )
    defaults = ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES
    sources.delete( :defaults )
  end
  if sources.include?( :jquery )
    sources.insert( 0, g.jquery_js ) unless sources.include?( g.jquery_js )
    sources.delete( :jquery )
  end
  if sources.include?( :mootools )
    unless sources.include?( g.mootools_js )
      insert_at = 0
      insert_at = 1 if( sources.include?( g.jquery_js ) )
      sources.insert( insert_at, g.mootools_js )
    end
    sources.delete( :mootools )
  end
  if defaults
    g.add_js_sources( *(sources << defaults) )
  else
    g.add_js_sources( *sources )
  end
  ''
end

#g_skin_tag(source, skin = 'default') ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/guilded/rails/view_helpers.rb', line 92

def g_skin_tag( source, skin='default' )
  path = "#{RAILS_ROOT}/public/stylesheets/#{GUILDED_JS_FOLDER}#{GUILDED_NS}#{source.to_s}/#{skin}.css"
  if File.exists?( path )
    stylesheet_link_tag( "/stylesheets/#{GUILDED_JS_FOLDER}#{GUILDED_NS}#{source.to_s}/#{skin}.css" )
  else
    ""
  end
end

Written to replace the Rails stylesheet_link_tag helper. Although the syntax is exactly the same, the method works a little differently.

This helper adds the stylesheet(s) to a collection to be renderred out together with all the guilded componenets stylesheets. This allows the stylesheets passed to this method to be cached with the guilded stylesheests into a single reusable file.

The helper will ensure that these stylesheets are included after Guilded’s reset stylesheet and before guilded’s component’s stylesheets so that they can override any resets, etc and not override any guilded components styles.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/guilded/rails/view_helpers.rb', line 80

def g_stylesheet_link_tag( *sources )
  options = sources.extract_options!
  g = Guilded::Guilder.instance
  
  if options[:ensure_primary]
    g.inject_css( *sources )
  else
    g.combined_css_srcs.push( *sources )
  end
  ''
end