jap_mag 0.0.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,177 +1,195 @@
1
1
  module JapMagWidgetsHelper
2
-
3
2
  def is_mobile_device?
4
3
  mobile_user_agents = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
5
4
  request.user_agent.to_s.downcase =~ Regexp.new(mobile_user_agents)
6
5
  end
7
-
6
+
8
7
  def get_body_class
9
8
  c = []
10
9
  c << controller.controller_name
11
10
  c << "mobile" if is_mobile_device?
12
-
11
+
13
12
  c.collect{|e| e.titleize.gsub(/\s/, "")}.join " "
14
13
  end
15
-
14
+
16
15
  def get_body_id
17
16
  content_for(:id).to_s.titleize.gsub(/\s/, "") unless content_for(:id).blank?
18
17
  end
19
-
18
+
20
19
  def get_wrapper_class
21
20
  c = []
22
21
  c << controller.action_name
23
-
22
+
24
23
  c.collect{|e| e.titleize.gsub(/\s/, "")}.join " "
25
24
  end
26
25
 
27
- #
28
- # navigation helper
29
- # by Felix
30
- #
31
- # the helper matches params with request.request_uri
32
- #
33
- # example:
34
- # sections = {
35
- # 'radio'=>{:title => 'Station', :url => radio_path},
36
- # 'edit_station'=>{:url => 'Radio', :url => edit_station_path}
37
- # }
38
- # options = {
39
- # :id => 'test_menu'
40
- # :current => 'current'
41
- # }
42
- # outputs:
43
- # <ul id="test_menu">
44
- # <li><a href="/station">Station</a></li>
45
- # <li class="current"><a href="/radio/">Radio</a></li>
46
- # </ul>
47
- #:title
48
- def menu(sections, options=nil)
49
- items = Array.new
50
-
51
- # default options
52
- options ||= Hash.new
53
- options[:id] ||= nil
54
- options[:class] ||= nil
55
- options[:current] ||= 'current' # jquery ui tabs plugin needs the later one
56
-
57
- sections.each do |section|
58
- klass = section[:current] ? "#{options[:current]} #{section[:class]}".strip : section[:class]
59
- items << content_tag(:li, link_to(section[:title], section[:url], :class => klass))
60
- end
26
+ def title *titles
27
+ seperator = " - "
61
28
 
62
- return content_tag :ul, :id => options[:id], :class => options[:class] do
63
- items.collect {|item| concat(item)}
64
- end
29
+ default_options = {sitename: _("/logo")}
30
+ options = titles.last.is_a?(Hash) ? titles.pop : {}
31
+ options = default_options.merge(options)
32
+ page_title = page_title_for_return = titles.join(seperator)
33
+ page_title = options[:sitename] + seperator + page_title_for_return if not options[:sitename].blank?
34
+
35
+ content_for :title, page_title
36
+
37
+ page_title_for_return
65
38
  end
66
39
 
67
- def clearfix options={}
68
- opt = {:class => " clearfix"}
69
- options[:class] += opt[:class] if options[:class]
70
- (options[:class] || opt[:class]).strip!
71
- opt = opt.merge(options)
72
- content_tag(:div, nil, opt)
40
+ def paginator collections, options = {}
41
+ will_paginate collections, options
73
42
  end
74
43
 
75
- def link_to_user(user, options={})
44
+ def last_deployed_at
45
+ file = File.join("tmp", "restart.txt")
46
+ File.exists?(file) ? File.atime(file) : nil
47
+ end
76
48
 
77
- default_options = {
78
- :href => user,
79
- :avatar => false,
80
- :title => user.name,
81
- :class => ""
82
- }
49
+ def bootstrap_menu links
50
+ links.collect do |link|
51
+ klass = []
52
+ klass << link[:klass] if link[:klass]
53
+ klass << "last-child" if link == links.last
83
54
 
84
- options = default_options.merge(options)
55
+ if (link[:controller_action].is_a?(Array) and current_controller_action_in?(*link[:controller_action])) \
56
+ or (current_controller_action_in?(link[:controller_action]))
57
+ klass << "active"
58
+ end
85
59
 
86
- if options[:avatar]
87
- return unless user.avatar?
60
+ klass = klass.empty? ? nil : klass.join(" ")
88
61
 
89
- text = image_tag(user.avatar.url(options[:avatar]), :alt => user.name, :class => "avatar")
62
+ content_tag :li, class: klass do
63
+ link_to link[:text], link[:path], link[:html_options]
64
+ end
65
+ end.join.html_safe
66
+ end
90
67
 
91
- options[:class] += " avatar".strip
92
- else
93
- text = user.name
94
- end
68
+ def bootstrap_scope_button scopes, options = {}
69
+ content_tag :ul, class: "nav-tabs simple-tabs" do
70
+ scopes.collect do |scope|
71
+ url = eval("#{options[:path].to_s}(scope: :#{scope[:key]})")
72
+ current = ((params[:scope] == scope[:key].to_s) or (scope[:default] and scope[:default] == true and params[:scope].blank?))
73
+ link = link_to("#{content_tag(:span, scope[:text])} (#{scope[:count]})".html_safe, url, class: 'nav-link')
74
+ klass = %w(nav-item)
75
+ klass << :active if current
95
76
 
96
- #raise url_for(user)#user_path(:subdomain => "aaa").inspect
97
- link_to(text, options[:href], :title => options[:title], :class => options[:class])
77
+ concat content_tag(:li, link, class: klass.join(' '))
78
+ end
79
+ end
98
80
  end
99
81
 
100
- def title(*titles)
101
- seperator = " - "
82
+ def bootstrap_button_group checkbox, id = nil, status = nil, default = nil
83
+ status ||= %w(turned_on turned_off)
84
+ default ||= "turned_off"
102
85
 
103
- default_options = {:sitename => true}
104
- options = titles.last.is_a?(Hash) ? titles.pop : {}
105
- options = default_options.merge(options)
86
+ content_tag :div, "id" => id, "class" => "bootstrap-toggle" do
87
+ buttons = content_tag :div, "class" => "btn-group", "data-toggle" => "buttons-radio" do
88
+ status.collect do |s|
89
+ content_tag :button, "type" => "button", "class" => "btn btn-default #{s}#{" active" if s == default}", "data-status" => s do
90
+ _("/page.my.pac.status.#{s}")
91
+ end
92
+ end.join.html_safe
93
+ end
106
94
 
107
- page_title = page_title_for_return = titles.join(seperator)
108
- page_title = t("logo") + seperator + page_title_for_return if options[:sitename]
95
+ buttons + checkbox
96
+ end
97
+ end
109
98
 
110
- content_for(:title, page_title)
99
+ def bootstrap_stacked_progress_bar percent, options = {}
100
+ klass = if percent < 50
101
+ :success
102
+ elsif percent >= 50 and percent < 80
103
+ :warning
104
+ else
105
+ :danger
106
+ end
111
107
 
112
- page_title_for_return
108
+ #percent = "#{percent * 100}%"
109
+ options[:precision] ||= 2
110
+ percent = number_to_percentage percent, options
111
+
112
+ opts_for_container = {class: :progress}
113
+ if options[:tooltip]
114
+ placement = options[:tooltip][:placement] || "top"
115
+ opts_for_container.merge!("data-toggle" => "tooltip", "data-placement" => placement, title: options[:tooltip][:title])
116
+ end
117
+
118
+ content_tag :div, opts_for_container do
119
+ content_tag :div, class: "progress-bar progress-bar-#{klass}", role: :progressbar, style: "width: #{percent}" do
120
+ percent
121
+ end
122
+ end
113
123
  end
114
124
 
115
- def random_disable_with_status
116
- status = [
117
- "Just a moment",
118
- "Let's stay together",
119
- "On the highway to hell",
120
- "Don't worry baby",
121
- "Let it be",
122
- "Getting you away from here",
123
- "Let's get it started",
124
- "It's now or never"
125
- ]
125
+ def link_to_external text, link, options = {}
126
+ options.merge!(target: :_blank)
127
+
128
+ if options[:anonymous]
129
+ link = "http://anonym.to/?#{link}"
130
+ options.delete(:anonymous)
131
+ end
132
+
133
+ link_to "#{text}#{fa_icon "external-link-alt", type: :fas}".html_safe, link, options
134
+ end
126
135
 
127
- status.shuffle.first + "..."
136
+ def input_for_selection text
137
+ (content_tag :input, nil, value: text, size: text.length, class: "form-input for-selection").html_safe
128
138
  end
129
139
 
130
140
  #
131
141
  # call to action
132
142
  #
133
- def cta text, url, html_options={}
134
-
135
- html_options[:class] = (html_options[:class].to_s + " cta").strip
143
+ def cta text, url, options = {}
144
+ klass = %w(button button-rounded button-caution)
145
+ klass << options[:class] if options[:class].present?
146
+ options[:class] = klass.join(" ")
136
147
 
137
- link_to content_tag(:span, text), url, html_options
148
+ link_to text, url, options
138
149
  end
139
150
 
140
- #
141
- # paginator
142
- #
143
- def paginator(collections, options={})
144
- will_paginate collections, options
151
+ def cta_params options = {}
152
+ {data: {disable_with: _("/actions.wait")}, class: "button button-rounded button-caution"}.merge options
145
153
  end
146
154
 
147
- def time_difference time
148
- "#{distance_of_time_in_words(time, Time.now, true)} ago" if time
155
+ def long_date date
156
+ I18n.l date, format: :long
149
157
  end
150
-
151
- def current_or_null(condition)
152
- condition ? "current" : nil
158
+
159
+
160
+ def short_date date
161
+ I18n.l date, format: :short
153
162
  end
154
163
 
155
- def scope_button scopes, options={}
156
- content_tag :ul, class: :scopes do
157
- scopes.collect do |scope|
158
- count = options[:count][scope[:key]]
159
- url = eval("#{options[:path].to_s}(scope: :#{scope[:key]})")
160
- current = ((params[:scope] == scope[:key].to_s) or (scope[:default] and params[:scope].blank?))
164
+ def retina_image_tag name_at_1x, options = {}
165
+ # webp for Chrome
166
+ if options[:webp]
167
+ name_at_1x = name_at_1x.gsub /\.[a-zA-Z]+$/, '.webp'
161
168
 
162
- concat content_tag(:li, link_to("#{content_tag(:span, scope[:text])} (#{count})".html_safe, url, class: current_or_null(current)))
163
- end
169
+ options.delete :webp
164
170
  end
165
- end
166
-
167
- def last_deployed_at
168
- file = File.join("tmp", "restart.txt")
169
- File.exists?(file) ? File.atime(file) : nil
171
+
172
+ # retina
173
+ name_at_2x = name_at_1x.gsub /\.\w+$/, "@2x\\0"
174
+
175
+ # i18n
176
+ if options[:i18n]
177
+ name_at_1x = name_at_1x.gsub /\.[a-zA-Z]+$/, ".#{I18n.locale}\\0"
178
+ name_at_2x = name_at_2x.gsub /\.[a-zA-Z]+$/, ".#{I18n.locale}\\0"
179
+
180
+ options.delete :i18n
181
+ end
182
+
183
+ # HTML 5 specific tag attributes
184
+ srcset = "#{asset_path(name_at_1x)} 1x, #{asset_path(name_at_2x)} 2x"
185
+ options = options.merge(srcset: srcset)
186
+
187
+ image_tag name_at_1x, options
170
188
  end
171
189
 
172
190
  protected
173
-
191
+
174
192
  def current_template
175
193
  File.basename(lookup_context.find_all(params[:action], params[:controller]).first.inspect)
176
194
  end
177
- end
195
+ end
@@ -1,13 +0,0 @@
1
- <div id="footer">
2
- <div class="gutter">
3
- <p><%= t :copyright %></p>
4
- <%= clearfix %>
5
- <ul class="languages">
6
- <%
7
- p = params.dup.delete_if{|key| %w(action controller locale).include?(key)}
8
- %>
9
- <li class="en"><%= link_to_unless_current t(:english), locale: :en, params: p %></li>
10
- <li class="zh-CN"><%= link_to_unless_current t(:chinese), locale: "zh-CN", params: p %></li>
11
- </ul>
12
- </div>
13
- </div>
@@ -1,7 +0,0 @@
1
- <div id="header">
2
- <div class="gutter">
3
- <h1><%= link_to t("logo"), root_path %></h1>
4
- <p class="slogan"><%= t("slogan") %></p>
5
- <%= render "layouts/navigation" %>
6
- </div>
7
- </div>
@@ -1,3 +0,0 @@
1
- <ul class="navigation">
2
- <li><%= link_to_unless_current t("navigation.home"), root_path %></li>
3
- </ul>
@@ -0,0 +1,34 @@
1
+ !!! 5
2
+ -# paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
3
+ <!--[if IE 8 ]> <html class="ie8" lang="zh-CN"> <![endif]-->
4
+ <!--[if IE 9 ]> <html class="ie9" lang="zh-CN"> <![endif]-->
5
+ <!--[if (gt IE 9)|!(IE)]><!--> <html lang="zh-CN"> <!--<![endif]-->
6
+ %head
7
+ %meta{ :charset => "utf-8" }/
8
+
9
+ -# Always force latest IE rendering engine (even in intranet) & Chrome Frame
10
+ -# Remove this if you use the .htaccess
11
+ %meta{ "http-equiv" => "X-UA-Compatible", :content => "IE=edge,chrome=1" }/
12
+
13
+ %title
14
+ = content_for(:title)
15
+ %meta{ :name => "description", :content => "" }/
16
+ %meta{ :name => "keywords", :content => "" }/
17
+ %meta{ :name => "viewport", :content => "width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no" }/
18
+ = favicon_link_tag "/favicon.ico"
19
+ = stylesheet_link_tag "application", media: "all", 'data-turbolinks-track' => true
20
+ = javascript_include_tag "application", 'data-turbolinks-track' => true
21
+ = csrf_meta_tags
22
+
23
+ %body{class: get_body_class, id: get_body_id}
24
+ #wrapper{class: "#{get_wrapper_class} container"}
25
+ = render partial: "layouts/header"
26
+
27
+ #body
28
+ = render partial: "layouts/flash", locals: {flash: flash}
29
+ = content_for(:content).blank? ? yield : yield(:content)
30
+
31
+ %footer#footer
32
+ = render partial: "layouts/footer"
33
+ = render "layouts/tracking"
34
+ = render "layouts/javascript"
@@ -2,9 +2,9 @@
2
2
  require File.expand_path('../lib/jap_mag/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Felix Ding"]
5
+ gem.authors = ["DING Yu"]
6
6
  gem.email = ["felixding@gmail.com"]
7
- gem.description = %q{JapMag is the design language created by Felix Ding. This gem helps designers start a project that follows JapMag.}
7
+ gem.description = %q{JapMag is a collection of frequently-used Rails controller methods and helpers.}
8
8
  gem.summary = "jap_mag-#{gem.version}"
9
9
  gem.homepage = "https://github.com/felixding/JapMag"
10
10
 
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "jap_mag"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = JapMag::VERSION
17
+
18
+ gem.add_runtime_dependency :will_paginate
17
19
  end
@@ -3,38 +3,86 @@ module JapMag
3
3
  def _ key, options={}
4
4
  # for absolute pathes /
5
5
  if (key[0] == "/")
6
- key = key.gsub("/", "")
7
-
8
- # for all other cases
6
+ key = key.sub("/", "")
7
+
8
+ # for all other cases
9
9
  else
10
10
  key = "#{params[:controller]}.#{params[:action]}.#{key}"
11
11
  end
12
12
 
13
13
  t(key, options)
14
14
  end
15
-
15
+
16
+ def current_locale
17
+ params[:locale] || I18n.default_locale || extract_locale_from_accept_language_header
18
+ end
19
+
20
+ def extract_locale_from_accept_language_header
21
+ if request.env and request.env['HTTP_ACCEPT_LANGUAGE']
22
+ lang = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
23
+ else
24
+ lang = nil
25
+ end
26
+
27
+ lang == "zh" ? "zh-CN" : "en"
28
+ end
29
+
30
+ def current_path
31
+ URI.encode(request.fullpath)
32
+ end
33
+
34
+ def current_url
35
+ URI.encode(request.original_url)
36
+ end
37
+
38
+ #
39
+ # the args can be one of the following:
40
+ #
41
+ # String: "page#index"
42
+ # Array: %w(page#index page#intro)
43
+ # multiple arguments: "page#index", "page#intro"
44
+ #
16
45
  def current_controller_action_in?(*args)
17
46
  controller = params["controller"]
18
47
  action = params["action"]
19
48
 
20
- args.each do |element|
49
+ if args.size == 1
50
+ if args.first.is_a?(Array)
51
+ arguments = args.first.split(" ").first
52
+ elsif args.first.is_a?(String)
53
+ if args.first.split(" ").size > 1
54
+ arguments = args.first.split(" ")
55
+ else
56
+ arguments = args
57
+ end
58
+ end
59
+ else
60
+ arguments = args
61
+ end
62
+
63
+ #raise arguments.inspect
64
+
65
+ arguments.each do |element|
21
66
  if element.include?("#")
22
- array = element.match(/([a-z\-\_]*)#([a-z\-\_]*)/).to_a
67
+ array = element.match(/([a-z\-\_\/]*)#([a-z\-\_]*)/).to_a
23
68
  c, a = array[1], array[2]
24
69
  return true if controller == c && action == a
25
70
  else
26
71
  return true if controller == element
27
72
  end
28
73
  end
29
-
74
+
30
75
  false
31
76
  end
32
-
77
+
33
78
  def self.included base
34
79
  base.helper_method :_
35
80
  base.helper_method :current_controller_action_in?
81
+ base.helper_method :current_locale
82
+ base.helper_method :current_url
83
+ base.helper_method :current_path
36
84
  end
37
-
85
+
38
86
  class Engine < Rails::Engine
39
87
  end
40
88
  end