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 @to_style: (hash) ->
\r
42 a = _.map hash, (n, k) ->
\r
43 Pettanr.to_s(k) + ': ' + Pettanr.to_s(n)
\r
46 @url: (controller_name, action_name, params) ->
\r
47 controller = Manifest.manifest().controllers[controller_name]
\r
48 action = controller.actions[action_name]
\r
51 class Pettanr.AppHelper
\r
55 @full_url: (filename) ->
\r
56 request.protocol + request.host_with_port + filename
\r
59 l = label.split('.')
\r
64 I18n.t('activerecord.models.' + Pettanr[label].item_name())
\r
66 Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last)
\r
68 @t_a: (item_name, attr_name) ->
\r
69 I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)
\r
71 @t_select_items: (items) ->
\r
73 [I18n.t(i[0]), i[1]]
\r
75 @t_selected_item: (name, index) ->
\r
76 I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])
\r
78 @distance_of_time_in_words_to_now: (datetime) ->
\r
81 class Pettanr.CounterModel extends Backbone.Model
\r
82 initialize: (attr, options) ->
\r
83 @url = options['url']
\r
88 class Pettanr.PageStatus
\r
90 constructor: (options) ->
\r
91 @action = options['action']
\r
92 @params = options['params']
\r
93 @current_page = parseInt(@params['page']) || 1
\r
94 @per_page = @params['page_size'] || 25
\r
96 @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)})
\r
98 class Pettanr.FilerCollection extends Backbone.Collection
\r
100 initialize: (models, options) ->
\r
101 @model = options['model']
\r
102 @action = options['action']
\r
103 @params = options['params']
\r
105 @url = '/' + @action.url(@params)
\r
107 class Pettanr.AppController
\r
110 @operators = window.operators
\r
113 @my_controller = Manifest.manifest().controllers[@params['controller']]
\r
114 @my_action = @my_controller.actions[@params['action']]
\r
115 @my_model = Manifest.manifest().models[@my_action.item_name]
\r
116 @my_model_class = @my_model.classify()
\r
120 @my_list_model = Manifest.manifest().models[@my_action.item_name]
\r
121 @my_list_model_class = @my_list_model.classify()
\r
123 model: @my_list_model_class,
\r
124 action: @my_action,
\r
127 @page_status = new Pettanr.PageStatus(@list_options)
\r
128 @list = new Pettanr.FilerCollection({}, @list_options)
\r
132 @item = new @my_model_class({id: @params['id']})
\r
136 @pager = new Locmare.FilerModule.DefaultPager({page_status: @page_status})
\r
137 f = new Locmare.Filer({
\r
139 item_name: @my_list_model_class.item_name(),
\r
140 collection: @list,
\r
142 operators: @operators
\r
146 profiler = new Locmare.Profiler({
\r
148 item_name: @item.item_name(),
\r
150 operators: @operators
\r
155 @item = new @my_model_class({id: @params['id']})
\r
156 @item.boosts 'post'
\r
157 @item.supply_default()
\r
161 @item = new @my_model_class({id: @params['id']})
\r
163 @item.fetch().done ->
\r
164 _this.item.boosts 'post'
\r
165 _this.render_form()
\r
168 @form = new Locmare.Form({
\r
169 form_name: @item.item_name(),
\r
172 submit: 'default',
\r
173 operators: @operators,
\r
174 action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))
\r
176 $("#pettanr").html(@form.render().el)
\r
185 class Pettanr.FilerRender
\r
186 constructor: (item_name, list_result, pager_type, operators) ->
\r
187 @item_name = item_name
\r
188 @list_result = list_result
\r
189 @pager_type = pager_type
\r
190 @operators = operators
\r
192 @list_result.fetch({}).done () ->
\r
196 items = @list_result.models
\r
197 f = new Locmare.Filer({
\r
198 item_name: @item_name,
\r
200 list_result: @list_result,
\r
201 pager_type: @pager_type,
\r
202 operators: @operators
\r
204 $("#pettanr").html(f.render().el)
\r
206 #>> https://gist.github.com/davidjbeveridge/3813724
\r
207 @xeach: (arr, func, index=0) ->
\r
208 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
210 @camelize: (input) ->
\r
211 pieces = input.split(/[\W_-]/)
\r
212 @xeach(pieces, @capitalize).join("")
\r
214 @capitalize: (input) ->
\r
215 input.charAt(0).toUpperCase() + input.slice(1)
\r
217 @lowercase: (input) ->
\r
218 input.toLowerCase()
\r
220 @underscore: (input) ->
\r
221 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
222 @xeach(pieces, @lowercase ).join("_")
\r