[PATCH/foreman 1/1] Feature #1524 Add more attributes to JSON host index

Signed-off-by: Corey Osman <corey@logicminds.biz>

··· --- app/controllers/hosts_controller.rb | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
index 55d388b…aefaa48 100644
— a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -39,7 +39,9 @@ class HostsController < ApplicationController
# rendering index page for non index page requests (out of sync hosts etc)
render :index if title and @title = title
end

  •  format.json { render :json => search.all(:select => "hosts.name", :include => included_associations).map(&:name) }
    
  •  # should you ever need more attributes just add to the :only array or specify :methods, :include, :except to the options hash
    
  •  format.json { render :json => search.all.to_json({:only => ["name", "id", "hostgroup_id", "operatingsystem_id"], :include => included_associations})}
    
  •  format.yaml do
       render :text => if params["rundeck"]
         result = {}
    


1.7.1

>
> Signed-off-by: Corey Osman <corey@logicminds.biz>
> —
> app/controllers/hosts_controller.rb | 4 ++±
> 1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
> index 55d388b…aefaa48 100644
> — a/app/controllers/hosts_controller.rb
> +++ b/app/controllers/hosts_controller.rb
> @@ -39,7 +39,9 @@ class HostsController < ApplicationController
> # rendering index page for non index page requests (out of sync hosts etc)
> render :index if title and @title = title
> end
> - format.json { render :json => search.all(:select => "hosts.name", :include => included_associations).map(&:name) }
> + # should you ever need more attributes just add to the :only array or specify :methods, :include, :except to the options hash
> + format.json { render :json => search.all.to_json({:only => ["name", "id", "hostgroup_id", "operatingsystem_id"], :include => included_associations})}
> +

would it make more sense to use the as_json method on the model?

··· On Mon, Mar 12, 2012 at 11:21 AM, Corey Osman wrote:
  format.yaml do
    render :text => if params["rundeck"]
      result = {}


1.7.1

Corey Osman
corey@logicminds.biz
678-348-0582 (Pacific Time)

Green IT and Datacenter Automation Specialist

>>
>> Signed-off-by: Corey Osman <corey@logicminds.biz>
>> —
>> app/controllers/hosts_controller.rb | 4 ++±
>> 1 files changed, 3 insertions(+), 1 deletions(-)
>>
>> diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
>> index 55d388b…aefaa48 100644
>> — a/app/controllers/hosts_controller.rb
>> +++ b/app/controllers/hosts_controller.rb
>> @@ -39,7 +39,9 @@ class HostsController < ApplicationController
>> # rendering index page for non index page requests (out of sync hosts etc)
>> render :index if title and @title = title
>> end
>> - format.json { render :json => search.all(:select => "hosts.name", :include => included_associations).map(&:name) }
>> + # should you ever need more attributes just add to the :only array or specify :methods, :include, :except to the options hash
>> + format.json { render :json => search.all.to_json({:only => ["name", "id", "hostgroup_id", "operatingsystem_id"], :include => included_associations})}
>> +
>
> would it make more sense to use the as_json method on the model?

I thought to_json was the public method and as_json was the private method in which to_json called in case it was overridden in the model.
I did look at overriding the as_json method but it didn't make sense since the json output can very depending on the using index or show controller methods.

Since I want different output from the index and show methods I can't hard code a default value in the as_json method, unless I rely on some arguments like (usedetailed, or useshort) which would represent a different set of output attributes.

··· On Mar 12, 2012, at 12:12 PM, Ohad Levy wrote: > On Mon, Mar 12, 2012 at 11:21 AM, Corey Osman wrote:
  format.yaml do
    render :text => if params["rundeck"]
      result = {}


1.7.1