Class: Stencils::StencilContext
- Inherits:
-
Object
- Object
- Stencils::StencilContext
- Defined in:
- lib/stencils/stencil_context.rb
Instance Attribute Summary collapse
-
#base_name ⇒ Object
Returns the value of attribute base_name.
-
#stencil_name ⇒ Object
Returns the value of attribute stencil_name.
-
#stencil_view_dir ⇒ Object
Returns the value of attribute stencil_view_dir.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #adjust_hash_arg_to_last(*args) ⇒ Object
- #base_name_for(model_or_class_or_symbol) ⇒ Object
- #collection ⇒ Object
- #collection=(collection) ⇒ Object (also: #set_collection)
- #collection_class_name ⇒ Object
- #collection_instance_variable_name ⇒ Object
- #collection_label ⇒ Object
- #collection_name ⇒ Object
- #collection_name_base ⇒ Object
- #collection_partial ⇒ Object
- #collection_path(options = nil) ⇒ Object
- #collection_route ⇒ Object
- #collection_symbol ⇒ Object
- #collection_url(options = nil) ⇒ Object
- #create_model_with_params ⇒ Object
- #destroy_model_with_param_id ⇒ Object
- #edit_path(model = self.model, options = nil) ⇒ Object
- #edit_path_url(model = self.model, options = nil) ⇒ Object
- #edit_route ⇒ Object
-
#find_model_with_param_id ⇒ Object
UTILS MOVED THESE OUT OF RestControllerExtension so they could be used separately.
- #fixture_model(name = nil) ⇒ Object
- #form_partial ⇒ Object
- #get_path_prefix ⇒ Object
- #init_model_with_params ⇒ Object
-
#initialize(template, model_or_class_or_symbol, options = {}) ⇒ StencilContext
constructor
A new instance of StencilContext.
- #instance_variable_get(name) ⇒ Object
- #instance_variable_set(name, value) ⇒ Object
- #link_to_collection(options = nil) ⇒ Object
- #model ⇒ Object
- #model=(model) ⇒ Object (also: #set_model)
- #model_class ⇒ Object
- #model_class_name ⇒ Object
- #model_instance_variable_name ⇒ Object
- #model_label ⇒ Object
- #model_name ⇒ Object
- #model_params ⇒ Object
- #model_partial ⇒ Object
- #model_symbol ⇒ Object
- #new_model ⇒ Object
- #new_model_with_params ⇒ Object
- #new_or_edit_label(new_label = "Add New #{model_label}", edit_label = "Edit #{model_label}") ⇒ Object
- #new_or_old_label(new_label, old_label) ⇒ Object
- #new_path(options = nil) ⇒ Object
- #new_path_url(options = nil) ⇒ Object
- #new_record? ⇒ Boolean
- #new_route ⇒ Object
- #params ⇒ Object
- #partial(name) ⇒ Object
- #path_prefix ⇒ Object
- #render_collection ⇒ Object
- #render_collection_partial(model) ⇒ Object
- #render_form(form) ⇒ Object
- #render_model ⇒ Object
- #save_model ⇒ Object
- #show_path(model = self.model, options = nil) ⇒ Object
- #show_route ⇒ Object
- #show_url(model = self.model, options = nil) ⇒ Object
- #uncountable?(name) ⇒ Boolean
- #update_model_with_params ⇒ Object
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, ={}) @template = template @base_name = base_name_for(model_or_class_or_symbol) @stencil_name = .delete :name @stencil_view_dir = .delete :view_dir end |
Instance Attribute Details
#base_name ⇒ Object
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_name ⇒ Object
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_dir ⇒ Object
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 |
#template ⇒ Object
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 |
#collection ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
117 118 119 |
# File 'lib/stencils/stencil_context.rb', line 117 def collection_instance_variable_name "@#{collection_name}" end |
#collection_label ⇒ Object
48 49 50 |
# File 'lib/stencils/stencil_context.rb', line 48 def collection_label collection_name.titleize end |
#collection_name ⇒ Object
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_base ⇒ Object
38 39 40 |
# File 'lib/stencils/stencil_context.rb', line 38 def collection_name_base collection_class_name.underscore.downcase end |
#collection_partial ⇒ Object
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(=nil) args = adjust_hash_arg_to_last 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_route ⇒ Object
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_symbol ⇒ Object
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(=nil) args = adjust_hash_arg_to_last 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_params ⇒ Object
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_id ⇒ Object
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, =nil) args = adjust_hash_arg_to_last model, 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, =nil) args = adjust_hash_arg_to_last model, args.pop if args.last && args.last.empty? args.delete_if {|arg| arg == nil} template.send "#{edit_route}_url", *args end |
#edit_route ⇒ Object
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_id ⇒ Object
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_partial ⇒ Object
85 86 87 |
# File 'lib/stencils/stencil_context.rb', line 85 def form_partial partial 'form' end |
#get_path_prefix ⇒ Object
209 210 211 |
# File 'lib/stencils/stencil_context.rb', line 209 def get_path_prefix path_prefix ? "#{path_prefix}_" : nil end |
#init_model_with_params ⇒ Object
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 |
#link_to_collection(options = nil) ⇒ Object
141 142 143 |
# File 'lib/stencils/stencil_context.rb', line 141 def link_to_collection(=nil) template.link_to(collection_label, collection_path()) end |
#model ⇒ Object
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_class ⇒ Object
236 237 238 |
# File 'lib/stencils/stencil_context.rb', line 236 def model_class Kernel.const_get(model_class_name) end |
#model_class_name ⇒ Object
93 94 95 |
# File 'lib/stencils/stencil_context.rb', line 93 def model_class_name collection_class_name.singularize end |
#model_instance_variable_name ⇒ Object
113 114 115 |
# File 'lib/stencils/stencil_context.rb', line 113 def model_instance_variable_name "@#{model_name}" end |
#model_label ⇒ Object
101 102 103 |
# File 'lib/stencils/stencil_context.rb', line 101 def model_label model_class_name.titleize end |
#model_name ⇒ Object
97 98 99 |
# File 'lib/stencils/stencil_context.rb', line 97 def model_name model_class_name.underscore.downcase end |
#model_params ⇒ Object
307 308 309 |
# File 'lib/stencils/stencil_context.rb', line 307 def model_params params[model_symbol] end |
#model_partial ⇒ Object
76 77 78 79 |
# File 'lib/stencils/stencil_context.rb', line 76 def model_partial # partial model_name partial 'model' end |
#model_symbol ⇒ Object
244 245 246 |
# File 'lib/stencils/stencil_context.rb', line 244 def model_symbol model_name.intern end |
#new_model ⇒ Object
273 274 275 |
# File 'lib/stencils/stencil_context.rb', line 273 def new_model self.set_model model_class.new end |
#new_model_with_params ⇒ Object
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(=nil) route = "#{new_route}_path" if .blank? template.send route else template.send route, 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(=nil) route = "#{new_route}_url" if .blank? template.send route else template.send route, end end |
#new_record? ⇒ Boolean
303 304 305 |
# File 'lib/stencils/stencil_context.rb', line 303 def new_record? model.new_record? end |
#new_route ⇒ Object
187 188 189 |
# File 'lib/stencils/stencil_context.rb', line 187 def new_route "new_#{get_path_prefix}#{model_name}" end |
#params ⇒ Object
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_prefix ⇒ Object
213 214 215 |
# File 'lib/stencils/stencil_context.rb', line 213 def path_prefix nil end |
#render_collection ⇒ Object
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_model ⇒ Object
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_model ⇒ Object
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, =nil) args = adjust_hash_arg_to_last model, 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_route ⇒ Object
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, =nil) args = adjust_hash_arg_to_last model, args.pop if args.last && args.last.empty? args.delete_if {|arg| arg == nil} template.send "#{show_route}_url", *args end |
#uncountable?(name) ⇒ 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_params ⇒ Object
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 |