From abe31b3c2eca5e2c96cec5e2dc18ded268d1bbf8 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 9 Oct 2012 18:46:20 +0400 Subject: [PATCH] #671: added API #update action for platform --- app/controllers/api/v1/platforms_controller.rb | 18 ++++++++++++++++++ app/models/platform.rb | 3 ++- app/views/api/v1/platforms/index.json.jbuilder | 2 +- app/views/api/v1/platforms/show.json.jbuilder | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 361b9f9bd..b27a98513 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -18,4 +18,22 @@ class Api::V1::PlatformsController < Api::V1::BaseController @platforms = Platform.main.opened.paginate(:page => params[:page], :per_page => 20) render :index end + + def update + p = params[:platform] || {} + owner = User.where(:id => p[:owner_id]).first + platform_params = {} + platform_params[:owner] = owner if owner + platform_params[:released] = p[:released] if p[:released] + platform_params[:description] = p[:description] if p[:description] + if @platform.update_attributes(p) + render :json => { + :platform => {:id => @platform.id, + :message => 'Platform has been updated successfully'} + }.to_json + else + render :json => {:message => "Validation Failed", :errors => @platform.errors.messages}.to_json, :status => 422 + end + end + end diff --git a/app/models/platform.rb b/app/models/platform.rb index 2dedbbde4..e43db852b 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -20,6 +20,7 @@ class Platform < ActiveRecord::Base has_many :mass_builds validates :description, :presence => true + validates :owner, :presence => true validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\.]+$/ } validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']} @@ -167,7 +168,7 @@ class Platform < ActiveRecord::Base def update_owner_relation if owner_id_was != owner_id - r = relations.where(:actor_id => owner_id_was, :actor_type => owner_type_was)[0] + r = relations.where(:actor_id => owner_id_was, :actor_type => owner_type_was).first r.update_attributes(:actor_id => owner_id, :actor_type => owner_type) end end diff --git a/app/views/api/v1/platforms/index.json.jbuilder b/app/views/api/v1/platforms/index.json.jbuilder index ac3d0dc3a..1a7298f12 100644 --- a/app/views/api/v1/platforms/index.json.jbuilder +++ b/app/views/api/v1/platforms/index.json.jbuilder @@ -7,7 +7,7 @@ json.platforms @platforms do |json, platform| end json.repositories platform.repositories do |json_repos, repo| json_repos.(repo, :id, :name) - json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.url api_v1_repository_path(repo.id, :format => :json) end json.url api_v1_platform_path(platform.name, :format => :json) end diff --git a/app/views/api/v1/platforms/show.json.jbuilder b/app/views/api/v1/platforms/show.json.jbuilder index 21a1b07f0..05be542b8 100644 --- a/app/views/api/v1/platforms/show.json.jbuilder +++ b/app/views/api/v1/platforms/show.json.jbuilder @@ -9,7 +9,7 @@ json.platform do |json| end json.repositories @platform.repositories do |json_repos, repo| json_repos.(repo, :id, :name) - json_repos.url api_v1_repository_path(repo.name, :format => :json) + json_repos.url api_v1_repository_path(repo.id, :format => :json) end end json.url api_v1_platform_path(@platform, :format => :json)