2 # Foo.class -> Pettanr[@constructor.name]
\r
7 @is_a_string: (str) ->
\r
8 if (str instanceof String) or (typeof str == "string")
\r
13 @is_number: (str) ->
\r
14 if typeof(str) != 'number' && typeof(str) != 'string'
\r
17 n = if typeof(str) == 'string'
\r
24 if @is_a_string(str)
\r
33 if @is_a_string(str)
\r
41 @truncate: (str, length = null) ->
\r
42 s = Pettanr.to_s(str)
\r
43 if length and s.length > 3 # 3 = '...'
\r
44 r = s.substr(0, length - 4)
\r
45 r = r.concat( '...') if s.length > length - 3
\r
50 @format_date: (date) ->
\r
51 y = ('000' + (date.getFullYear())).slice(-4)
\r
52 m = ('0' + (date.getMonth() + 1)).slice(-2)
\r
53 d = ('0' + date.getDate()).slice(-2)
\r
54 h = ('0' + date.getHours()).slice(-2)
\r
55 n = ('0' + date.getMinutes()).slice(-2)
\r
56 s = ('0' + date.getSeconds()).slice(-2)
\r
57 y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s
\r
59 @to_style: (hash) ->
\r
60 a = _.map hash, (n, k) ->
\r
61 Pettanr.to_s(k) + ': ' + Pettanr.to_s(n)
\r
64 @url: (controller_name, action_name, params) ->
\r
65 if controller = Manifest.manifest().controllers[controller_name]
\r
66 # convert from manifest
\r
67 action = controller.actions[action_name]
\r
71 controller_name + '/' + action_name
\r
73 @params_to_url: (params) ->
\r
74 return '' if params['controller'] == 'folders' and params['action'] == 'root'
\r
75 @url(params['controller'], params['action'], params)
\r
78 Manifest.manifest().magic_numbers.run_mode != 0
\r
80 class Pettanr.AppHelper
\r
84 @full_url: (filename) ->
\r
85 request.protocol + request.host_with_port + filename
\r
88 l = label.split('.')
\r
93 I18n.t('activerecord.models.' + Pettanr[label].item_name())
\r
95 Pettanr.AppHelper.t_a(Pettanr[l[0]].item_name(), l[1])
\r
97 @t_a: (item_name, attr_name) ->
\r
98 I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)
\r
100 @t_select_items: (items) ->
\r
101 _.map items, (i) ->
\r
102 [I18n.t(i[0]), i[1]]
\r
104 @t_selected_item: (name, index) ->
\r
105 I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])
\r
107 @distance_of_time_in_words_to_now: (datetime) ->
\r
108 if Pettanr.is_blank(datetime)
\r
111 $.timeago(datetime)
\r
116 #>> https://gist.github.com/davidjbeveridge/3813724
\r
117 @xeach: (arr, func, index=0) ->
\r
118 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
120 @camelize: (input) ->
\r
121 pieces = input.split(/[\W_-]/)
\r
122 @xeach(pieces, @capitalize).join("")
\r
124 @capitalize: (input) ->
\r
125 input.charAt(0).toUpperCase() + input.slice(1)
\r
127 @lowercase: (input) ->
\r
128 input.toLowerCase()
\r
130 @underscore: (input) ->
\r
131 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
132 @xeach(pieces, @lowercase ).join("_")
\r