Module: Hovercraft::Routes

Defined in:
lib/hovercraft/routes.rb

Instance Method Summary collapse

Instance Method Details

#generate_create(model_class, model_name, plural_model_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/hovercraft/routes.rb', line 11

def generate_create(model_class, model_name, plural_model_name)
  post(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
    model_instance = model_class.create(params[model_name.to_sym])
    status model_instance.valid? ? 201 : 400
    respond_with model_instance
  end
end

#generate_destroy(model_class, model_name, plural_model_name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/hovercraft/routes.rb', line 36

def generate_destroy(model_class, model_name, plural_model_name)
  delete(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
    model_instance = model_class.find(params[:id])
    model_instance.destroy
    status 204
    respond_with model_instance
  end
end

#generate_index(model_class, model_name, plural_model_name) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/hovercraft/routes.rb', line 3

def generate_index(model_class, model_name, plural_model_name)
  get(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
    model_instances = model_class.all
    status 200
    respond_with model_instances
  end
end

#generate_show(model_class, model_name, plural_model_name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/hovercraft/routes.rb', line 19

def generate_show(model_class, model_name, plural_model_name)
  get(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
    model_instance = model_class.find(params[:id])
    status 200
    respond_with model_instance
  end
end

#generate_update(model_class, model_name, plural_model_name) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/hovercraft/routes.rb', line 27

def generate_update(model_class, model_name, plural_model_name)
  put(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
    model_instance = model_class.find(params[:id])
    model_instance.update_attributes(params[model_name.to_sym])
    status model_instance.valid? ? 204 : 400
    respond_with model_instance
  end
end