Module: Styler::Helper

Defined in:
lib/styler/helper.rb

Instance Method Summary collapse

Instance Method Details

#add_stylesheet(*stylesheets) ⇒ Object

Allows individual views to customize the stylesheet list



102
103
104
105
106
107
# File 'lib/styler/helper.rb', line 102

def add_stylesheet(*stylesheets)
  @stylesheets_add ||= []
  stylesheets.each do |stylesheet|
    @stylesheets_add << stylesheet
  end
end

#ignore_stylesheet(*stylesheets) ⇒ Object

Allows individual views to customize the stylesheet list



110
111
112
113
114
115
# File 'lib/styler/helper.rb', line 110

def ignore_stylesheet(*stylesheets)
  @stylesheets_ignore ||= []
  stylesheets.each do |stylesheet|
    @stylesheets_ignore << stylesheet
  end
end

#page_stylesheets(options = {}) ⇒ Object

Include Stylesheets on the page options to an array of strings for stylesheets to include options sets an array of strings for stylesheets to exclude

Automatically includes stylesheets (if they exist on filesystem)

  • stylesheets for [controller]/[controller].css and [controller]/[action].css
  • stylesheets for browser overrides (ie6.css, ie7.css, safari.css)


35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/styler/helper.rb', line 35

def page_stylesheets(options={})

  # set default values
  stylesheets = []
  @stylesheets_add ||= []
  @stylesheets_ignore ||= []

  # Include sheets from options
  if options[:include]
    @stylesheets_add = [options[:include]].flatten + @stylesheets_add
  end

  # Exclude sheets from options
  if options[:exclude]
    @stylesheets_ignore = [options[:exclude]].flatten + @stylesheets_ignore
  end

  # Add all sheets from instance variable
  @stylesheets_add.each do |stylesheet|
    stylesheets << stylesheet
  end

  # Ignore all sheets from instance variable
  @stylesheets_ignore.each do |stylesheet|
    stylesheets.delete(stylesheet)
  end

  # dedupe stylesheets array
  stylesheets.uniq!

  # Build Initial Output
  html_output = stylesheet_link_tag(*stylesheets)

  # Build conditional stylesheets (we need to check these on the filesystem before including)
  conditional_stylesheets = []

  # Controller/action sheets
  conditional_stylesheets << "pages/#{controller.controller_name}"
  conditional_stylesheets << "pages/#{controller.controller_name}/#{controller.action_name}"
    
  # IE6
  if browser_is?(:ie6)
    conditional_stylesheets << "ie6"
  end

  # IE7
  if browser_is?(:ie7)
    conditional_stylesheets << "ie7"
  end
  
  # Safari
  if browser_is?(:safari) and 
    conditional_stylesheets << "safari"
  end  

  # only add conditional stylesheets if they exist on disk
  conditional_stylesheets.each do |stylesheet|
    if File.exist?("#{RAILS_ROOT}/public/stylesheets/#{stylesheet}.css")
      html_output << "\n"
      html_output << stylesheet_link_tag(stylesheet)
    end
  end        

  return html_output
end

#stylesheet(*input) ⇒ Object

alias for stylesheet_link_tag



4
5
6
7
# File 'lib/styler/helper.rb', line 4

def stylesheet(*input)
  input = [input].flatten
  stylesheet_link_tag(*input)
end

#stylesheet_folder(path) ⇒ Object

returns an entire directory of stylesheets recursively



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/styler/helper.rb', line 15

def stylesheet_folder(path)
  if use_cache? # or browser_is? :ie
    return stylesheet("min/#{path}.css")
  else
    result = []
    Dir["#{Rails.public_path}/stylesheets/#{path}/**/*.css"].each do |css|
      result << css.gsub("#{Rails.public_path}/stylesheets/", "")
    end
    return stylesheet(result)
  end
end

#stylesheets(options = {}) ⇒ Object

override this in your application_helper to clean it up



10
11
12
# File 'lib/styler/helper.rb', line 10

def stylesheets(options = {})
  [stylesheet(:defaults), page_stylesheets(options)].join("\n")
end