Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jia Brown
committed
Mar 24, 2012
1 parent
a474529
commit 275d583
Showing
2 changed files
with
46 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,52 @@ | ||
class DevicesController < ApplicationController | ||
|
||
before_filter :find_device, :except => [:new, :create] | ||
before_filter :expand_details, :only => [:show, :edit] | ||
before_filter :flatten_details, :only => [:create, :update] | ||
|
||
def new | ||
@device = Device.new(:category => params["category"]) | ||
@groups = DeviceInformation.groups(params["category"]) | ||
end | ||
|
||
def create | ||
details = [] | ||
|
||
params["details"].each do |group, fields| | ||
fields.each do |name, value| | ||
details << { :group => group, :name => name, :value => value } | ||
end | ||
end | ||
|
||
device = Device.create(params["device"].merge(:details_attributes => details, :room_id => params["room_id"])) | ||
device = Device.create(device_params) | ||
redirect_to room_device_path(params["room_id"], device.id) | ||
end | ||
|
||
def show | ||
def update | ||
@device.update_attributes(device_params) | ||
redirect_to room_device_path(params["room_id"], @device.id) | ||
end | ||
|
||
private | ||
|
||
def find_device | ||
@device = Device.find(params["id"]) | ||
end | ||
|
||
def expand_details | ||
@groups = DeviceInformation.groups(@device.category) | ||
|
||
@details = Hash.new { |h,k| h[k] = {} } | ||
|
||
@device.details.each do |d| | ||
@details[d.group][d.name] = d.value | ||
end | ||
end | ||
|
||
def flatten_details | ||
@details = [] | ||
|
||
params["details"].each do |group, fields| | ||
fields.each do |name, value| | ||
@details << { :group => group, :name => name, :value => value } | ||
end | ||
end | ||
end | ||
|
||
def device_params | ||
params["device"].merge(:details_attributes => @details, :room_id => params["room_id"]) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
= form_for(@device, :url => room_device_path(@device), :method => :put) do |f| | ||
= f.label :name | ||
= f.text_field :name | ||
= f.hidden_field :category | ||
|
||
- @groups.each do |group| | ||
%h3= group.name | ||
- group.fields.each do |field_name| | ||
%p | ||
= label_tag "details[#{group.name}][#{field_name}]", "#{field_name}:" | ||
= text_field_tag "details[#{group.name}][#{field_name}]", | ||
@details[group.name][field_name] | ||
= submit_tag |