Module: Hammock::RestfulActions

Defined in:
lib/hammock/restful_actions.rb

Constant Summary collapse

MixInto =
ApplicationController

Instance Method Summary collapse

Instance Method Details

#createObject

The create action. (POST, unsafe, non-idempotent)

Creates a new record with the supplied attributes. TODO



28
29
30
31
32
33
34
# File 'lib/hammock/restful_actions.rb', line 28

def create
  if !find_record_on_create && !assign_createable
    escort :unauthed
  else
    render_or_redirect_after save_record
  end
end

#destroyObject

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.



68
69
70
71
72
73
# File 'lib/hammock/restful_actions.rb', line 68

def destroy
  if find_record
    result = callback(:before_destroy) and @record.destroy and callback(:after_destroy)
    render_for_destroy result
  end
end

#editObject

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.



50
51
52
53
54
# File 'lib/hammock/restful_actions.rb', line 50

def edit
  if find_record
    render_for_safe_actions if callback(:before_modify) and callback(:before_edit)
  end
end

#indexObject

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.



8
9
10
# File 'lib/hammock/restful_actions.rb', line 8

def index
  render_for_index if tasks_for_index
end

#newObject

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.



17
18
19
20
21
22
23
# File 'lib/hammock/restful_actions.rb', line 17

def new
  if !tasks_for_new
    escort :unauthed
  else
    render_for_safe_actions
  end
end

#showObject

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.



39
40
41
42
43
# File 'lib/hammock/restful_actions.rb', line 39

def show
  if find_record
    render_for_safe_actions if callback(:before_show)
  end
end

#suggestObject

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.



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
# File 'lib/hammock/restful_actions.rb', line 88

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, :limit => params[:limit].to_i, :order => @order
    end
  end
  
  if @results.nil?
    escort_for_bad_request
  else
    callback(:after_suggest)
    respond_to do |format|
      format.html { render :action => "suggest_#{fields.join(',')}", :layout => false }
      format.json { render :json => @results }
    end
  end
end

#undestroyObject

The undestroy action. (POST, unsafe, non-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.



78
79
80
81
82
83
# File 'lib/hammock/restful_actions.rb', line 78

def undestroy
  if find_destroyed_record
    result = callback(:before_undestroy) and @record.restore and callback(:after_undestroy)
    render_for_destroy result
  end
end

#updateObject

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.



59
60
61
62
63
# File 'lib/hammock/restful_actions.rb', line 59

def update
  if find_record
    render_or_redirect_after assign_and_save_record
  end
end