Implemented work package update with some child resources

pull/1435/head
Marek Takac 11 years ago
parent 17edd10bb5
commit 2ab0bea6ac
  1. 40
      app/api/work_packages/work_package_model.rb
  2. 4
      app/api/work_packages/work_package_representer.rb
  3. 18
      app/api/work_packages/work_packages.rb

@ -12,6 +12,9 @@ module WorkPackages
property :created_at, on: :work_package
property :updated_at, on: :work_package
property :author, on: :work_package
property :project_id, on: :work_package
property :responsible_id, on: :work_package
property :assigned_to_id, on: :work_package
def type
work_package.type.name
@ -40,41 +43,28 @@ module WorkPackages
work_package.priority = priority
end
def responsible_login
work_package.responsible.try(:login)
end
def responsible_login=(value)
responsible_user = User.find(:first, conditions: ['login ilike ?', value])
work_package.responsible = responsible_user
end
def responsible
work_package.responsible
end
def assignee_login
work_package.assigned_to.try(:login)
def estimated_time
{ units: 'hours', value: work_package.estimated_hours }
end
def assignee_login=(value)
assignee_user = User.find(:first, conditions: ['login ilike ?', value])
work_package.assigned_to = assignee_user
def estimated_time=(value)
hours = ActiveSupport::JSON.decode(value)['value']
work_package.estimated_hours = hours
end
def assignee
work_package.assigned_to
def project_id=(value)
work_package.project_id = value
end
def estimated_time
{ units: 'hours', value: work_package.estimated_hours }
def responsible_id=(value)
work_package.responsible_id = value
end
def estimated_time=(value)
hours = ActiveSupport::JSON.decode(value)['value']
work_package.estimated_hours = hours
def assigned_to_id=(value)
work_package.assigned_to_id = value
end
validates :subject, presence: true
validates_inclusion_of :project_id, in: Project.pluck(:id)
end
end

@ -26,12 +26,12 @@ module WorkPackages
property :estimated_time
property :percentage_done
property :project_id, getter: lambda { |*| work_package.project.id }
property :project_name, getter: lambda { |*| work_package.project.name }
property :project_name, getter: lambda { |*| work_package.project.try(:name) }
property :responsible_id, getter: lambda { |*| work_package.responsible.try(:id) }, render_nil: true
property :responsible_name, getter: lambda { |*| work_package.responsible.try(:name) }, render_nil: true
property :responsible_login, getter: lambda { |*| work_package.responsible.try(:login) }, render_nil: true
property :responsible_mail, getter: lambda { |*| work_package.responsible.try(:mail) }, render_nil: true
property :assignee_id, getter: lambda { |*| work_package.assigned_to.try(:id) }, render_nil: true
property :assigned_to_id, as: :assignee_id, getter: lambda { |*| work_package.assigned_to.try(:id) }, render_nil: true
property :assignee_name, getter: lambda { |*| work_package.assigned_to.try(:name) }, render_nil: true
property :assignee_login, getter: lambda { |*| work_package.assigned_to.try(:login) }, render_nil: true
property :assignee_mail, getter: lambda { |*| work_package.assigned_to.try(:mail) }, render_nil: true

@ -38,16 +38,18 @@ module WorkPackages
'update a work package (provide whole resource)'
end
params do
optional :project_id, type: Integer, desc: 'Project id'
optional :responsible_id, type: Integer, desc: 'Responsible user id'
end
patch do
work_package_model = WorkPackageModel.new(work_package: @work_package)
work_package_representer = WorkPackageRepresenter.new(work_package_model)
work_package_representer.from_json(request.POST.to_json)
if work_package_representer.represented.valid?
work_package_representer.represented.save!
work_package_representer.to_json
params.delete(:id)
@work_package_representer.from_json(params.to_json)
if @work_package_representer.represented.valid?
@work_package_representer.represented.save!
@work_package_representer.to_json
else
work_package_representer.represented.errors.to_json
@work_package_representer.represented.errors.to_json
end
end

Loading…
Cancel
Save