Class: Ext::Resource::Controllers::Rest

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/resource.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.modelObject

Returns the value of attribute model.



22
23
24
# File 'lib/ext/resource.rb', line 22

def model
  @model
end

.pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/ext/resource.rb', line 22

def path
  @path
end

Class Method Details

.inherited(c) ⇒ Object



32
33
34
# File 'lib/ext/resource.rb', line 32

def inherited(c)
  c.path = "/#{c.name.gsub(/^.*::/,'').downcase}"
end

.urlsObject



24
25
26
27
28
29
30
# File 'lib/ext/resource.rb', line 24

def urls
  return [] unless @path
  @__urls  ||= [
    "#{path}", "#{path}/(\\d+)", "#{path}/(\\w+)",
    "#{path}/(\\w+)/(\\d+)", "#{path}/(\d+)/(\\w+)"
  ]
end

Instance Method Details

#createObject

CRUD operations ###



90
91
92
# File 'lib/ext/resource.rb', line 90

def create # POST
  render name + '_create'
end

#delete(id) ⇒ Object



86
# File 'lib/ext/resource.rb', line 86

def delete(id); destroy(id); end

#destroy(id) ⇒ Object

DELETE



102
103
104
# File 'lib/ext/resource.rb', line 102

def destroy(id) # DELETE
  redirect R(self, :index)
end

#editObject



116
117
118
# File 'lib/ext/resource.rb', line 116

def edit
  render name + '_edit'
end

#get(action = nil, id = nil) ⇒ Object

GET method dispatching TODO : Test this method



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ext/resource.rb', line 40

def get(action=nil, id=nil)
  # corrections
  if (new_id = action.to_i) > 0
    action = id ? id : 'show'
    id = new_id
  elsif not action
    action = 'index'
  end

  if respond_to?(action) and not ['create','update','delete'].include?(action)
    if id and method(action).arity > 0
      return send(action, id)
    elsif method(action).arity < 1
      return send(action)
    end
  end
  puts "Action not found" if $DBG
  forward NotFound, 'get', @env.SCRIPT_INFO
end

#indexObject

Pages ###



108
109
110
# File 'lib/ext/resource.rb', line 108

def index
  render name + '_index'
end

#newObject



112
113
114
# File 'lib/ext/resource.rb', line 112

def new
  render name + '_new'
end

#post(action = nil, id = nil) ⇒ Object

POST method dispatching TODO : Update this method



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ext/resource.rb', line 62

def post(action=nil, id=nil)
  # corrections
  if (new_id = action.to_i) > 0
    action = id ? id : 'update'
    id = new_id
  elsif not action
    action = 'create'
  end
  
  # prefix other actions
  action = '_' + action unless ['create','update','delete'].include?(action)

  if respond_to?(action)
    if id and method(action).arity > 0
      return send(action, id)
    elsif method(action).arity < 1
      return send(action)
    end
  end
  puts "Action not found" if $DBG 
  forward NotFound, 'get', @env.SCRIPT_INFO
end

#put(id) ⇒ Object



85
# File 'lib/ext/resource.rb', line 85

def put(id); update(id); end

#show(id) ⇒ Object

GET



94
95
96
# File 'lib/ext/resource.rb', line 94

def show(id) # GET
  render name + '_show'
end

#update(id) ⇒ Object

PUT



98
99
100
# File 'lib/ext/resource.rb', line 98

def update(id) # PUT
  redirect R(self, :show, id)
end