Class: Stencils::StencilContext

Inherits:
Object
  • Object
show all
Defined in:
lib/stencils/stencil_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, model_or_class_or_symbol, options = {}) ⇒ StencilContext

Returns a new instance of StencilContext.



9
10
11
12
13
14
# File 'lib/stencils/stencil_context.rb', line 9

def initialize(template, model_or_class_or_symbol, options={})
  @template = template
  @base_name = base_name_for(model_or_class_or_symbol)
  @stencil_name = options.delete :name
  @stencil_view_dir = options.delete :view_dir
end

Instance Attribute Details

#base_nameObject

Returns the value of attribute base_name.



6
7
8
# File 'lib/stencils/stencil_context.rb', line 6

def base_name
  @base_name
end

#stencil_nameObject

Returns the value of attribute stencil_name.



7
8
9
# File 'lib/stencils/stencil_context.rb', line 7

def stencil_name
  @stencil_name
end

#stencil_view_dirObject

Returns the value of attribute stencil_view_dir.



7
8
9
# File 'lib/stencils/stencil_context.rb', line 7

def stencil_view_dir
  @stencil_view_dir
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/stencils/stencil_context.rb', line 5

def template
  @template
end

Instance Method Details

#adjust_hash_arg_to_last(*args) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/stencils/stencil_context.rb', line 248

def adjust_hash_arg_to_last(*args)
  hash_arg = nil
  args.each_with_index do |arg,index|
    if arg.kind_of? Hash
      hash_arg = arg
    end
    if hash_arg 
      if index == args.size-1
        args[index] = hash_arg 
      else
        args[index] = nil 
      end
    end
  end
  args
end

#base_name_for(model_or_class_or_symbol) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stencils/stencil_context.rb', line 16

def base_name_for(model_or_class_or_symbol)
  full_base_name = if model_or_class_or_symbol.kind_of? Class
    model_or_class_or_symbol.name
  elsif model_or_class_or_symbol.kind_of? Symbol
    model_or_class_or_symbol.to_s.camelize
  # elsif model_or_class_or_symbol.respond_to?(:controller) 
  elsif model_or_class_or_symbol.respond_to?(:controller) 
    model_or_class_or_symbol.controller.class.name
  else
    model_or_class_or_symbol.class.name
  end
  name = full_base_name.chomp('Test').chomp('Controller')
  index = name.rindex(':')
  index = index ? index+1 : 0
  name[index,name.size]
end

#collectionObject



217
218
219
# File 'lib/stencils/stencil_context.rb', line 217

def collection
  instance_variable_get collection_instance_variable_name
end

#collection=(collection) ⇒ Object Also known as: set_collection



221
222
223
# File 'lib/stencils/stencil_context.rb', line 221

def collection=(collection)
  instance_variable_set collection_instance_variable_name, collection
end

#collection_class_nameObject



33
34
35
36
# File 'lib/stencils/stencil_context.rb', line 33

def collection_class_name
  #this singularize/pluralize chain lets you provide eg people/person and get people back, as opposed to peoples.
  base_name.singularize.pluralize
end

#collection_instance_variable_nameObject



117
118
119
# File 'lib/stencils/stencil_context.rb', line 117

def collection_instance_variable_name
  "@#{collection_name}"
end

#collection_labelObject



48
49
50
# File 'lib/stencils/stencil_context.rb', line 48

def collection_label
  collection_name.titleize
end

#collection_nameObject



42
43
44
45
46
# File 'lib/stencils/stencil_context.rb', line 42

def collection_name
  name = collection_name_base
  name = "#{name}_collection" if uncountable? name
  name
end

#collection_name_baseObject



38
39
40
# File 'lib/stencils/stencil_context.rb', line 38

def collection_name_base
  collection_class_name.underscore.downcase
end

#collection_partialObject



60
61
62
# File 'lib/stencils/stencil_context.rb', line 60

def collection_partial
  partial 'list_item'
end

#collection_path(options = nil) ⇒ Object



134
135
136
137
138
139
# File 'lib/stencils/stencil_context.rb', line 134

def collection_path(options=nil)
  args = adjust_hash_arg_to_last options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil || arg.kind_of?(ActiveRecord::Base)}
  template.send "#{collection_route}_path", *args
end

#collection_routeObject



121
122
123
124
125
# File 'lib/stencils/stencil_context.rb', line 121

def collection_route
  route = collection_name_base
  route = "#{route}_index" if uncountable? route
  route
end

#collection_symbolObject



52
53
54
# File 'lib/stencils/stencil_context.rb', line 52

def collection_symbol
  collection_name.intern
end

#collection_url(options = nil) ⇒ Object



127
128
129
130
131
132
# File 'lib/stencils/stencil_context.rb', line 127

def collection_url(options=nil)
  args = adjust_hash_arg_to_last options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil || arg.kind_of?(ActiveRecord::Base)}
  template.send "#{collection_route}_url", *args
end

#create_model_with_paramsObject



281
282
283
# File 'lib/stencils/stencil_context.rb', line 281

def create_model_with_params
  self.set_model model_class.create(model_params)
end

#destroy_model_with_param_idObject



295
296
297
# File 'lib/stencils/stencil_context.rb', line 295

def destroy_model_with_param_id
  find_model_with_param_id.destroy
end

#edit_path(model = self.model, options = nil) ⇒ Object



173
174
175
176
177
178
# File 'lib/stencils/stencil_context.rb', line 173

def edit_path(model=self.model, options=nil)
  args = adjust_hash_arg_to_last model, options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil}
  template.send "#{edit_route}_path", *args
end

#edit_path_url(model = self.model, options = nil) ⇒ Object



180
181
182
183
184
185
# File 'lib/stencils/stencil_context.rb', line 180

def edit_path_url(model=self.model, options=nil)
  args = adjust_hash_arg_to_last model, options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil}
  template.send "#{edit_route}_url", *args
end

#edit_routeObject



169
170
171
# File 'lib/stencils/stencil_context.rb', line 169

def edit_route
  "edit_#{get_path_prefix}#{model_name}"
end

#find_model_with_param_idObject

UTILS MOVED THESE OUT OF RestControllerExtension so they could be used separately



267
268
269
270
271
# File 'lib/stencils/stencil_context.rb', line 267

def find_model_with_param_id
  model_id = "#{model_name}_id"
  param = params[model_id] || params[:id]
  self.set_model model_class.find(param)
end

#fixture_model(name = nil) ⇒ Object



240
241
242
# File 'lib/stencils/stencil_context.rb', line 240

def fixture_model(name=nil)
  template.send collection_name_base, name
end

#form_partialObject



85
86
87
# File 'lib/stencils/stencil_context.rb', line 85

def form_partial
  partial 'form'
end

#get_path_prefixObject



209
210
211
# File 'lib/stencils/stencil_context.rb', line 209

def get_path_prefix
  path_prefix ? "#{path_prefix}_" : nil
end

#init_model_with_paramsObject



290
291
292
293
# File 'lib/stencils/stencil_context.rb', line 290

def init_model_with_params
  find_model_with_param_id
  self.model.attributes = model_params
end

#instance_variable_get(name) ⇒ Object



321
322
323
# File 'lib/stencils/stencil_context.rb', line 321

def instance_variable_get(name)
  super || template.instance_variable_get(name) || template.controller.instance_variable_get(name)
end

#instance_variable_set(name, value) ⇒ Object



315
316
317
318
319
# File 'lib/stencils/stencil_context.rb', line 315

def instance_variable_set(name, value)
  super
  template.instance_variable_set name, value
  template.controller.instance_variable_set name, value
end


141
142
143
# File 'lib/stencils/stencil_context.rb', line 141

def link_to_collection(options=nil)
  template.link_to(collection_label, collection_path(options))
end

#modelObject



226
227
228
# File 'lib/stencils/stencil_context.rb', line 226

def model
  instance_variable_get model_instance_variable_name
end

#model=(model) ⇒ Object Also known as: set_model



230
231
232
233
# File 'lib/stencils/stencil_context.rb', line 230

def model=(model)
  puts "***model= : #{model_instance_variable_name}"
  instance_variable_set model_instance_variable_name, model
end

#model_classObject



236
237
238
# File 'lib/stencils/stencil_context.rb', line 236

def model_class
  Kernel.const_get(model_class_name)
end

#model_class_nameObject



93
94
95
# File 'lib/stencils/stencil_context.rb', line 93

def model_class_name
  collection_class_name.singularize
end

#model_instance_variable_nameObject



113
114
115
# File 'lib/stencils/stencil_context.rb', line 113

def model_instance_variable_name
  "@#{model_name}"
end

#model_labelObject



101
102
103
# File 'lib/stencils/stencil_context.rb', line 101

def model_label
  model_class_name.titleize
end

#model_nameObject



97
98
99
# File 'lib/stencils/stencil_context.rb', line 97

def model_name
  model_class_name.underscore.downcase
end

#model_paramsObject



307
308
309
# File 'lib/stencils/stencil_context.rb', line 307

def model_params
  params[model_symbol]
end

#model_partialObject



76
77
78
79
# File 'lib/stencils/stencil_context.rb', line 76

def model_partial
  # partial model_name
  partial 'model'
end

#model_symbolObject



244
245
246
# File 'lib/stencils/stencil_context.rb', line 244

def model_symbol
  model_name.intern
end

#new_modelObject



273
274
275
# File 'lib/stencils/stencil_context.rb', line 273

def new_model
  self.set_model model_class.new
end

#new_model_with_paramsObject



277
278
279
# File 'lib/stencils/stencil_context.rb', line 277

def new_model_with_params
  self.set_model model_class.new(model_params)
end

#new_or_edit_label(new_label = "Add New #{model_label}", edit_label = "Edit #{model_label}") ⇒ Object



109
110
111
# File 'lib/stencils/stencil_context.rb', line 109

def new_or_edit_label(new_label="Add New #{model_label}", edit_label="Edit #{model_label}")
  new_or_old_label new_label, edit_label
end

#new_or_old_label(new_label, old_label) ⇒ Object



105
106
107
# File 'lib/stencils/stencil_context.rb', line 105

def new_or_old_label(new_label, old_label)
  new_record? ? new_label : old_label
end

#new_path(options = nil) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/stencils/stencil_context.rb', line 191

def new_path(options=nil)
  route = "#{new_route}_path"
  if options.blank? 
    template.send route
  else
    template.send route, options
  end
end

#new_path_url(options = nil) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/stencils/stencil_context.rb', line 200

def new_path_url(options=nil)
  route = "#{new_route}_url"
  if options.blank? 
    template.send route
  else
    template.send route, options
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/stencils/stencil_context.rb', line 303

def new_record?
  model.new_record?
end

#new_routeObject



187
188
189
# File 'lib/stencils/stencil_context.rb', line 187

def new_route
  "new_#{get_path_prefix}#{model_name}"
end

#paramsObject



311
312
313
# File 'lib/stencils/stencil_context.rb', line 311

def params
  template.params
end

#partial(name) ⇒ Object



56
57
58
# File 'lib/stencils/stencil_context.rb', line 56

def partial(name)
  "#{collection_name}/#{name}"
end

#path_prefixObject



213
214
215
# File 'lib/stencils/stencil_context.rb', line 213

def path_prefix
  nil
end

#render_collectionObject



68
69
70
71
72
73
74
# File 'lib/stencils/stencil_context.rb', line 68

def render_collection
  html = []
  collection.each do |model|
    html << render_collection_partial(model)
  end
  html.join
end

#render_collection_partial(model) ⇒ Object



64
65
66
# File 'lib/stencils/stencil_context.rb', line 64

def render_collection_partial(model)
  template.render :partial => collection_partial, :locals => {model_symbol => model}
end

#render_form(form) ⇒ Object



89
90
91
# File 'lib/stencils/stencil_context.rb', line 89

def render_form(form)
  template.render :partial => form_partial, :locals => {:form => form, model_symbol => model}
end

#render_modelObject



81
82
83
# File 'lib/stencils/stencil_context.rb', line 81

def render_model
  template.render :partial => model_partial, :locals => {model_symbol => model}
end

#save_modelObject



299
300
301
# File 'lib/stencils/stencil_context.rb', line 299

def save_model
  self.model.save
end

#show_path(model = self.model, options = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/stencils/stencil_context.rb', line 149

def show_path(model=self.model, options=nil)

  args = adjust_hash_arg_to_last model, options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil}

  puts "***show_path: "
  puts "***model: #{model.inspect}"
  puts "***args: #{args.inspect}"

  template.send "#{show_route}_path", *args
end

#show_routeObject



145
146
147
# File 'lib/stencils/stencil_context.rb', line 145

def show_route
  "#{get_path_prefix}#{model_name}"
end

#show_url(model = self.model, options = nil) ⇒ Object



162
163
164
165
166
167
# File 'lib/stencils/stencil_context.rb', line 162

def show_url(model=self.model, options=nil)
  args = adjust_hash_arg_to_last model, options
  args.pop if args.last && args.last.empty?
  args.delete_if {|arg| arg == nil}
  template.send "#{show_route}_url", *args
end

#uncountable?(name) ⇒ Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/stencils/stencil_context.rb', line 325

def uncountable?(name)
  ActiveSupport::Inflector.inflections.uncountables.include? name
end

#update_model_with_paramsObject



285
286
287
288
# File 'lib/stencils/stencil_context.rb', line 285

def update_model_with_params
  find_model_with_param_id
  self.model.update_attributes(model_params)
end