easel_helpers 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module JqueryHelper
4
+
5
+ def document_ready(&block)
6
+ html = content_tag :script, :type => "text/javascript" do
7
+ %(
8
+ (function($) {
9
+ $(document).ready(function() {
10
+ #{capture(&block)}
11
+ });
12
+ })(jQuery);
13
+ )
14
+ end
15
+
16
+ concat(html)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module LinkHelper
4
+
5
+ def link_button(*args, &block)
6
+ doc = Hpricot(link_to(*args, &block))
7
+ doc.at("a").inner_html = "<span>#{doc.at("a").inner_html}</span>"
8
+ (doc/:a).add_class("btn")
9
+ doc.to_html
10
+ end
11
+
12
+ def link_to_email(email_address, *args)
13
+ options = args.extract_options!
14
+ link = args.first.is_a?(String) ? h(args.first) : email_address
15
+ return link if email_address.blank?
16
+ link_to link, "mailto:#{email_address}", options
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module MessageHelper
4
+
5
+ def messages(messages, options = {})
6
+ except_keys = [options[:except]].flatten.compact
7
+ only_keys = [options[:only]].flatten.compact
8
+
9
+ if except_keys.any? && only_keys.any?
10
+ raise ArgumentError, ":only and :except options conflict; use one"
11
+ end
12
+
13
+ keys = if except_keys.any?
14
+ messages.keys - except_keys
15
+ elsif only_keys.any?
16
+ messages.keys & only_keys
17
+ else
18
+ messages.keys
19
+ end
20
+
21
+ keys.map do |key|
22
+ if messages[key].present?
23
+ content_tag :p,
24
+ messages[key],
25
+ :class => [key, "box", "single-line"].join(" ")
26
+ end
27
+ end.join
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module NavigationHelper
4
+ def tab(name, path, options = {}, li_options = {})
5
+ opts = parse_tab_options(name, li_options)
6
+
7
+ active = "active" if (opts[:active] == opts[:comparison]) || opts[:compare]
8
+ css_classes = [] << opts[:li_classes] << active
9
+ css_classes = clean_css_classes(css_classes)
10
+ li_options.merge!(:class => css_classes) if css_classes.present?
11
+
12
+ content_tag :li,
13
+ link_to(name, path, options),
14
+ li_options
15
+ end
16
+
17
+ private
18
+
19
+ def parse_tab_options(name, li_options = {})
20
+ returning({}) do |result|
21
+ result[:active] = li_options.delete(:active) || (name.gsub(/\s/, '').tableize || "")
22
+ result[:comparison] = li_options.delete(:active_compare) || controller.controller_name
23
+ result[:compare] = li_options.delete(:compare) || false
24
+ result[:li_classes] = li_options.delete(:class)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module RjsHelper
4
+
5
+ def inline_flash(page, flash, options = {})
6
+ container_id = options[:container] || "flash-container"
7
+ current_flash = flash
8
+
9
+ flash.discard unless options[:keep_flash]
10
+
11
+ if options[:replace]
12
+ page.replace_html container_id, messages(current_flash)
13
+ else
14
+ page.insert_html :top, container_id, messages(current_flash)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module StructureHelper
4
+
5
+ def blockquote(*args, &block)
6
+ options = args.extract_options!
7
+ author = options.delete(:author)
8
+ option_quote = textilize(options.delete(:quote))
9
+
10
+ bq = content_tag :blockquote,
11
+ option_quote.blank? ? capture(&block) : option_quote
12
+
13
+ html = if author
14
+ content_tag :div,
15
+ bq + content_tag(:cite, author),
16
+ :class => "quote-cited"
17
+ else
18
+ bq
19
+ end
20
+
21
+ concat(html)
22
+ end
23
+
24
+ def body(*args)
25
+ options = args.extract_options!
26
+ @_page_body_attributes ||= {}
27
+
28
+ css_classes = [] << @_page_body_attributes.delete(:class) << args
29
+
30
+ @_page_body_attributes = @_page_body_attributes.merge(options)
31
+ @_page_body_attributes[:class] = clean_css_classes(css_classes)
32
+
33
+ if block_given?
34
+ block = lambda { yield }
35
+
36
+ html = content_tag(:body, capture(&block), @_page_body_attributes)
37
+ concat(html)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ module EaselHelpers
2
+ module Helpers
3
+ module TableHelper
4
+
5
+ def zebra_row(options = {}, &block)
6
+ cycle_list = options.delete(:cycle_list) || [nil, "alt"]
7
+ css_classes = [cycle(*cycle_list)] << options.delete(:class)
8
+ css_classes = clean_css_classes(css_classes)
9
+
10
+ html = content_tag :tr,
11
+ capture(&block),
12
+ options.merge(:class => css_classes)
13
+ concat(html)
14
+ end
15
+
16
+ def recordset(*args, &block)
17
+ options = args.extract_options!
18
+ options[:table] ||= {}
19
+
20
+ headers = []
21
+ (options[:headers] || []).each_with_index do |header, index|
22
+ head = [header].flatten
23
+ opts = head.extract_options!
24
+
25
+ css_classes = [] << opts.delete(:class) << case index
26
+ when 0 then "first"
27
+ when (options[:headers].size - 1) then "last"
28
+ end
29
+
30
+ headers << if head.first =~ /^\<th/
31
+ th = Hpricot(head.first)
32
+ th_classes = th.at("th")["class"].join
33
+ th_classes = clean_css_classes([th_classes, css_classes])
34
+ th.at("th")["class"] = th_classes
35
+ th.to_html
36
+ else
37
+ content_tag :th,
38
+ head.first,
39
+ opts.merge(:class => clean_css_classes(css_classes))
40
+ end
41
+ end
42
+
43
+ table_classes = ["recordset", args] << options[:table].delete(:class)
44
+ css_classes = clean_css_classes(table_classes, {"last" => last_column})
45
+
46
+ html = clean_column(css_classes) do
47
+ table_options = options[:table]
48
+ table_options.merge!(:class => css_classes, :cellspacing => 0)
49
+ content_tag(:table,
50
+ content_tag(:thead, content_tag(:tr, headers.join)) + \
51
+ capture(&block),
52
+ table_options)
53
+ end
54
+
55
+ reset_cycle
56
+ concat(html)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ module ActionView
2
+ module Partials
3
+ private
4
+
5
+ def render_partial_with_easel(*args)
6
+ path = args.first[:partial]
7
+ locals = args.last || {}
8
+
9
+ easel_cached_column_counts = session[:easel_cached_column_counts] ||= {}
10
+
11
+ if easel_cached_column_counts.keys.include?(path)
12
+ @_easel_column_count = locals[:easel_width] || easel_cached_column_counts[path]
13
+ easel_cached_column_counts[path] = @_easel_column_count
14
+ else
15
+ if @_easel_column_count.is_a?(Fixnum) && path !~ /^layout/
16
+ easel_cached_column_counts[path] = @_easel_column_count
17
+ end
18
+ end
19
+
20
+ render_partial_without_easel(*args)
21
+ end
22
+
23
+ alias_method_chain :render_partial, :easel
24
+ end
25
+ end
26
+
27
+ module EaselHelpers
28
+ module PartialCaching
29
+ def self.included(base)
30
+ base.send :include, EaselHelpers::PartialCaching::InstanceMethods
31
+ base.before_filter :clear_easel_cache
32
+ end
33
+
34
+ module InstanceMethods
35
+ def clear_easel_cache
36
+ session[:easel_cached_column_counts] = nil unless request.xhr?
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ require "easel_helpers"
2
+
3
+ ActionView::Base.send :include, EaselHelpers::Helpers
4
+ ActionController::Base.send :include, EaselHelpers::PartialCaching
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :easel_helpers do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class DateHelperTest < ActiveSupport::TestCase
4
+ include EaselHelpers::Helpers::DateHelper
5
+
6
+ context "datetime helper" do
7
+ should "default to an empty string if date is not supplied" do
8
+ assert_equal "", datetime(nil)
9
+ end
10
+
11
+ should "default to passed default text if date is not supplied" do
12
+ assert_equal "default text", datetime(nil, "default text")
13
+ end
14
+
15
+ should "default to :long format for date" do
16
+ timestamp = Time.now
17
+ assert_equal timestamp.to_s(:long), datetime(timestamp)
18
+ end
19
+
20
+ should "use passed format if applicable" do
21
+ timestamp = Time.now
22
+ assert_equal timestamp.to_s(:short), datetime(timestamp, "", :short)
23
+ end
24
+
25
+ should "convert passed data to a time" do
26
+ datestamp = Date.today
27
+ timestamp = datestamp.to_time
28
+ assert_equal timestamp.to_s(:long), datetime(datestamp)
29
+ end
30
+ end
31
+
32
+ context "date helper" do
33
+ should "default to an empty string if date is not supplied" do
34
+ assert_equal "", date(nil)
35
+ end
36
+
37
+ should "default to passed default text if date is not supplied" do
38
+ assert_equal "default text", date(nil, "default text")
39
+ end
40
+
41
+ should "default to :long format for date" do
42
+ datestamp = Date.today
43
+ assert_equal datestamp.to_s(:long), date(datestamp)
44
+ end
45
+
46
+ should "use passed format if applicable" do
47
+ datestamp = Date.today
48
+ assert_equal datestamp.to_s(:short), date(datestamp, "", :short)
49
+ end
50
+
51
+ should "convert passed data to a time" do
52
+ timestamp = Time.now
53
+ datestamp = timestamp.to_date
54
+
55
+ assert_equal datestamp.to_s(:long), date(timestamp)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+
3
+ class FormHelperTest < EaselHelpers::ViewTestCase
4
+
5
+ context "submit_button" do
6
+
7
+ should "default with the correct structure" do
8
+ show_view "<%= submit_button 'Create' %>" do
9
+ assert_select "button.btn[type=submit][value=Create]" do
10
+ assert_select "span", "Create"
11
+ end
12
+ end
13
+ end
14
+
15
+ should "allow adding additional classes" do
16
+ show_view "<%= submit_button 'Create', 'adtl-class', :dumb %>" do
17
+ assert_select "button.btn.adtl-class.dumb[type=submit][value=Create]" do
18
+ assert_select "span", "Create"
19
+ end
20
+ end
21
+ end
22
+
23
+ should "handle additional attributes set" do
24
+ show_view "<%= submit_button 'Create', :kls, :id => 'my-id', :type => 'image' %>" do
25
+ assert_select "button.btn.kls#my-id[type=image][value=Create]" do
26
+ assert_select "span", "Create"
27
+ end
28
+ end
29
+ end
30
+
31
+ should "allow overriding of value" do
32
+ show_view "<%= submit_button 'Create', :value => 'override' %>" do
33
+ assert_select "button.btn[type=submit][value=override]" do
34
+ assert_select "span", "Create"
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ context "set" do
42
+
43
+ should "default with the correct structure" do
44
+ show_view "<% set do %>words<% end %>" do
45
+ assert_select "div.text", "words"
46
+ end
47
+ end
48
+
49
+ should "allow adding/overriding classes" do
50
+ show_view "<% set :checkbox do %>words<% end %>" do
51
+ assert_select "div.checkbox", "words"
52
+ end
53
+ end
54
+
55
+ should "handle additional attributes set" do
56
+ show_view "<% set :id => 'custom-id' do %>words<% end %>" do
57
+ assert_select "div.text#custom-id", "words"
58
+ end
59
+ end
60
+
61
+ should "assign default class if width class is passed as the only class" do
62
+ show_view "<% set :half do %>words<% end %>" do
63
+ assert_select "div.text.col-12", "words"
64
+ end
65
+ end
66
+
67
+ should "assign default class if last class is passed as the only class" do
68
+ show_view "<% set :half, :last do %>words<% end %>" do
69
+ assert_select "div.col-12.text.col-last", "words"
70
+ end
71
+ end
72
+
73
+ should "assign default class if error class is passed" do
74
+ show_view "<% set :half, :last, :error do %>words<% end %>" do
75
+ assert_select "div.col-12.text.col-last.error", "words"
76
+ end
77
+ end
78
+
79
+ should "assign default class if the class textarea is present" do
80
+ show_view "<% set :textarea do %>words<% end %>" do
81
+ assert_select "div.textarea.text", "words"
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ context "fieldset" do
88
+
89
+ should "default with the correct structure" do
90
+ show_view "<% fieldset do %>words<% end %>" do
91
+ assert_select "fieldset", "words"
92
+ end
93
+ end
94
+
95
+ should "assign the first argument as the legend if it is a string" do
96
+ show_view "<% fieldset 'User Information' do %>words<% end %>" do
97
+ assert_select "fieldset", /words/ do
98
+ assert_select "h3.legend", "User Information"
99
+ end
100
+ end
101
+ end
102
+
103
+ should "allow adding fieldset classes" do
104
+ show_view "<% fieldset :hform, 'col-last' do %>words<% end %>" do
105
+ assert_select "fieldset.hform.col-last", "words"
106
+ end
107
+ end
108
+
109
+ should "allow adding fieldset classes and a legend" do
110
+ show_view "<% fieldset 'User Information', :hform, 'col-last' do %>words<% end %>" do
111
+ assert_select "fieldset.hform.col-last", /words/ do
112
+ assert_select "h3.legend", "User Information"
113
+ end
114
+ end
115
+ end
116
+
117
+ should "allow assignment of legend attributes" do
118
+ show_view "<% fieldset 'User Information', :hform, :legend => {:class => 'lgnd', :id => 'legend-id'} do %>words<% end %>" do
119
+ assert_select "fieldset.hform", /words/ do
120
+ assert_select "h3.legend.lgnd#legend-id", "User Information"
121
+ end
122
+ end
123
+ end
124
+
125
+ should "allow assignment of fieldset attributes" do
126
+ show_view "<% fieldset 'User Information', :hform, :id => 'my-fieldset' do %>words<% end %>" do
127
+ assert_select "fieldset.hform#my-fieldset", /words/ do
128
+ assert_select "h3.legend", "User Information"
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+
135
+ end