Module: Hammock::RestfulActions::InstanceMethods
- Defined in:
- lib/hammock/restful_actions.rb
Instance Method Summary collapse
-
#create ⇒ Object
The
create
action. -
#destroy ⇒ Object
The
destroy
action. -
#edit ⇒ Object
The
edit
action. -
#index ⇒ Object
The
index
action. -
#new ⇒ Object
The
new
action. -
#show ⇒ Object
The
show
action. -
#suggest ⇒ Object
The
suggest
action. -
#undestroy ⇒ Object
The
undestroy
action. -
#update ⇒ Object
The
update
action.
Instance Method Details
#create ⇒ Object
The create
action. (POST, unsafe, non-idempotent)
Creates a new record with the supplied attributes. TODO
43 44 45 46 47 48 49 |
# File 'lib/hammock/restful_actions.rb', line 43 def create if !find_record_on_create && !assign_createable escort :unauthed else render_or_redirect_after save_record end end |
#destroy ⇒ Object
The destroy
action. (DELETE, unsafe, non-idempotent)
Destroys the specified record if it is within the current write scope, defined by write_scope
and write_scope_for
on the current model.
90 91 92 93 94 95 |
# File 'lib/hammock/restful_actions.rb', line 90 def destroy if find_record result = callback(:before_destroy) and @record.destroy and callback(:after_destroy) render_for_destroy result end end |
#edit ⇒ Object
The edit
action. (GET, safe, idempotent)
Renders a form containing the fields populated with the current record’s attributes.
This action is available within the same scope as update
, since it is only useful if a subsequent update
would be successful.
65 66 67 68 69 |
# File 'lib/hammock/restful_actions.rb', line 65 def edit if find_record render_for_safe_actions if callback(:before_modify) and callback(:before_edit) end end |
#index ⇒ Object
The index
action. (GET, safe, idempotent)
Lists the current resource’s records that are visible within the current index scope, defined by index_scope
and index_scope_for
on the current model.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hammock/restful_actions.rb', line 16 def index if tasks_for_index respond_to do |format| format.html format.xml { render :xml => @records.kick } format.json { render :json => @records.kick } format.yaml { render :text => @records.kick.to_yaml } end end end |
#new ⇒ Object
The new
action. (GET, safe, idempotent)
Renders a form containing the required fields to create a new record.
This action is available within the same scope as create
, since it is only useful if a subsequent create
would be successful.
32 33 34 35 36 37 38 |
# File 'lib/hammock/restful_actions.rb', line 32 def new if !tasks_for_new escort :unauthed else render_for_safe_actions end end |
#show ⇒ Object
The show
action. (GET, safe, idempotent)
Displays the specified record if it is within the current read scope, defined by read_scope
and read_scope_for
on the current model.
54 55 56 57 58 |
# File 'lib/hammock/restful_actions.rb', line 54 def show if find_record render_for_safe_actions if callback(:before_show) end end |
#suggest ⇒ Object
The suggest
action. (GET, safe, idempotent)
Lists the current resource’s records that would be listed by an index, filtered to show only those where at least one of the specified keys matches each whitespace-separated term in the query.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/hammock/restful_actions.rb', line 110 def suggest @results = if params[:q].blank? log 'No query specified.' elsif params[:fields].blank? log "No fields specified." elsif !callback(:before_suggest) # fail else fields = params[:fields].split(',') @queries = params[:q].downcase.split(/\s+/) mdl.send :with_scope, :find => suggest_scope.to_hash do mdl.suggest fields, @queries end end if @results.nil? escort_for_bad_request else callback(:after_suggest) render :action => "suggest_#{fields.join(',')}", :layout => false end end |
#undestroy ⇒ Object
The undestroy
action. (POST, unsafe, idempotent)
Reverses a previous destroy on the specified record if it is within the current write scope, defined by write_scope
and write_scope_for
on the current model.
100 101 102 103 104 105 |
# File 'lib/hammock/restful_actions.rb', line 100 def undestroy if find_deleted_record result = callback(:before_undestroy) and @record.undestroy and callback(:after_undestroy) render_for_destroy result end end |
#update ⇒ Object
The update
action. (PUT, unsafe, idempotent)
Updates the specified record with the supplied attributes if it is within the current write scope, defined by write_scope
and write_scope_for
on the current model.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hammock/restful_actions.rb', line 74 def update if find_record # If params[:attribute] is given, update only that attribute. We mass-assign either way to filter through attr_accessible. @record.attributes = if (attribute_name = params[:attribute]) { attribute_name => params_for(mdl.symbolize)[attribute_name] } else params_for mdl.symbolize end render_or_redirect_after save_record end end |