Module: Ramaze::Helper::Crud
- Defined in:
- lib/cortex_reaver/helper/crud.rb
Overview
Provides list, create, read, update, and delete functionality for Cortex Reaver. Centralizes the lookup and database logic for re-use across several models.
Requires pagination, auth, workflow, and feeds
Class Method Summary collapse
Instance Method Summary collapse
- #delete(id) ⇒ Object
- #edit(id = nil) ⇒ Object
-
#index(id = nil) ⇒ Object
Normal actions start here.
- #page(page = nil) ⇒ Object
- #show(id = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/cortex_reaver/helper/crud.rb', line 13 def self.included(base) base.instance_eval do # Provides on_save, on_create, on_update callbacks for controllers, # which defines how the controller CRUD sets attributes on the # primary model. # This block is called when a new model is being created. The model # and request are passed to the block. The resulting model is stored # in the database. Example: # # JournalController.on_save do |journal, request| # journal.title = request[:title] # journal.body = request[:body].downcase # end # # You can also define on_save, on_second_save, and on_update methods # which will be called after the blocks. The difference is that the # block forms are evaluated in the class, and the method forms are # evaluated in the context of the instance. def self.on_create(&block) unless block_given? and block.arity == 2 raise ArgumentError.new("needs a block with two arguments") end @on_create = block end def self.on_update(&block) unless block_given? and block.arity == 2 raise ArgumentError.new("needs a block with two arguments") end @on_update = block end def self.on_save(&block) unless block_given? and block.arity == 2 raise ArgumentError.new("needs a block with two arguments") end @on_save = block end def self.on_second_save(&block) unless block_given? and block.arity == 2 raise ArgumentError.new("needs a block with two arguments") end @on_second_save = block end def self.on_create_block @on_create end def self.on_save_block @on_save end def self.on_second_save_block @on_second_save end def self.on_update_block @on_update end end base.class_eval do # Check that MODEL is defined, so we know what we're working with. unless const_defined?('MODEL') raise RuntimeError.new("can't use CRUD helper unless MODEL is set.") end # Set the singular and plural instance vars for MODEL. unless const_defined?('SINGULAR_MODEL_VAR') const_set('SINGULAR_MODEL_VAR', '@' + const_get('MODEL').to_s.demodulize.underscore) end unless const_defined?('PLURAL_MODEL_VAR') const_set('PLURAL_MODEL_VAR', const_get('SINGULAR_MODEL_VAR').pluralize) end # This action can't be in the normal module body, or it breaks things. def new # You need to be able to create one of these. for_auth do |u| u.can_create? model_class.new end @title = "New #{model_class.to_s.demodulize.titleize}" @form_action = :new if request.post? begin # Create model CortexReaver.db.transaction do @model = model_class.new # Initial callbacks if block = self.class.on_save_block block.call(@model, request) end if respond_to? :on_save on_save(@model, request) end if block = self.class.on_create_block block.call(@model, request) end if respond_to? :on_create on_create(@model, request) end # Save for the first time raise unless @model.save # Second save callback, if applicable if block = self.class.on_second_save_block block.call(@model, request) end if respond_to? :on_second_save on_second_save @model, request end if block or respond_to? :on_second_save raise unless @model.save end flash[:notice] = "Created #{model_class.to_s.demodulize.downcase} #{h @model.to_s}." redirect @model.url end rescue => e # An error occurred Ramaze::Log.error e.inspect + e.backtrace.join("\n") if e.is_a? Sequel::ValidationFailed flash[:error] = "Couldn't update #{model_class.to_s.demodulize.downcase} #{h @model.to_s}, because there were errors in the form." else flash[:error] = "Couldn't create #{model_class.to_s.demodulize.downcase} #{h request[:title]}: #{h e}." end set_singular_model_var @model end else set_singular_model_var model_class.new end end end end |
Instance Method Details
#delete(id) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cortex_reaver/helper/crud.rb', line 193 def delete(id) if @model = model_class[id] for_auth do |u| u.can_edit? @model end begin raise unless @model.destroy flash[:notice] = "#{model_class.to_s.demodulize.downcase} #{h @model.to_s} deleted." redirect rs() rescue => e flash[:notice] = "Couldn't delete #{model_class.to_s.demodulize.downcase} #{h @model.to_s}." if @model.errors.size > 0 flash[:notice] << ' ' + @model.errors.to_s else flash[:notice] << ' ' + e. end redirect @model.url end else flash[:error] = "No such #{model_class.to_s.demodulize.downcase} (#{h id}) exists." redirect rs() end end |
#edit(id = nil) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/cortex_reaver/helper/crud.rb', line 218 def edit(id = nil) if @model = model_class[id] for_auth do |u| u.can_edit? @model end @title = "Edit #{model_class.to_s.demodulize.downcase} #{@model.to_s}" @form_action = "edit/#{@model.id}" set_singular_model_var @model if request.post? begin # Update model CortexReaver.db.transaction do # Initial callbacks if block = self.class.on_save_block block.call(@model, request) end if respond_to? :on_save on_save @model, request end if block = self.class.on_update_block block.call(@model, request) end if respond_to? :on_update on_update @model, request end # Save raise unless @model.save # Second callbacks and save if applicable if block = self.class.on_second_save_block block.call(@model, request) end if respond_to? :on_second_save on_second_save @model, request end if block or respond_to? :on_second_save raise unless @model.save end # Invalidate caches Ramaze::Cache.action.clear flash[:notice] = "Updated #{model_class.to_s.demodulize.downcase} #{h @model.to_s}." redirect @model.url end rescue => e # An error occurred Ramaze::Log.error e.inspect + e.backtrace.join("\n") if e.is_a? Sequel::ValidationFailed flash[:error] = "Couldn't update #{model_class.to_s.demodulize.downcase} #{h @model.to_s}, because there were errors in the form." else flash[:error] = "Couldn't update #{model_class.to_s.demodulize.downcase} #{h @model.to_s}: #{h e}." end end end else flash[:error] = "No such #{model_class.to_s.demodulize.downcase} (#{id}) exists." redirect rs() end end |
#index(id = nil) ⇒ Object
Normal actions start here.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cortex_reaver/helper/crud.rb', line 179 def index(id = nil) if id # Redirect to show # # This way, you can (assuming your name doesn't conflict with an # existing action) tell people to visit /journals/my-cool-event, and # it will go to /journals/show/my-cool-event. raw_redirect rs(:show, id), :status => 301 else # Display index page :last end end |
#page(page = nil) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/cortex_reaver/helper/crud.rb', line 284 def page(page = nil) page = case page when Symbol page when 'first' :first when 'last' :last when nil error_404 else page.to_i end if page.is_a? Integer and (page < 0 or page > model_class.window_count) # This page isn't in the sequence! error_404 end @title = "#{model_class.to_s.demodulize.pluralize.titleize}" if self.class.private_method_defined? :feed and model_class.respond_to? :atom_url feed @title, model_class.atom_url end @models = model_class.window(page) if @models.respond_to? :viewable_by @models = @models.viewable_by(user) end @page = page set_plural_model_var @models if user.can_create? model_class.new workflow "New #{model_class.to_s.demodulize}", rs(:new), :new, model_class.to_s.demodulize.downcase end render_view(:list) end |
#show(id = nil) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/cortex_reaver/helper/crud.rb', line 324 def show(id = nil) if id and @model = model_class.get(id) # Found that model unless user.can_view? @model error_403 end # Redirect IDs to names raw_redirect(@model.url, :status => 301) if id =~ /^\d+$/ @title = @model.to_s set_singular_model_var @model if @model.class.associations.include? :comments # Retrieve pending comment from session, if applicable. if comment = session[:pending_comment] and comment.parent == @model @new_comment = session.delete :pending_comment else # Create a comment to be posted @new_comment = CortexReaver::Comment.new @new_comment.send("#{model_class.to_s.demodulize.underscore.downcase}=", @model) end if session[:user] @new_comment.creator = session[:user] end end if user.can_create? model_class.new workflow "New #{model_class.to_s.demodulize}", rs(:new), :new, model_class.to_s.demodulize.downcase end if user.can_edit? @model workflow "Edit this #{model_class.to_s.demodulize}", rs(:edit, @model.id), :edit, model_class.to_s.demodulize.downcase end if user.can_delete? @model workflow "Delete this #{model_class.to_s.demodulize}", rs(:delete, @model.id), :delete, model_class.to_s.demodulize.downcase end elsif id # Didn't find that model error_404 end end |