Module: Api::Customfields

Extended by:
ActiveSupport::Concern
Included in:
ProjectsController
Defined in:
app/controllers/concerns/api/customfields.rb

Instance Method Summary collapse

Instance Method Details

#create_custom_dataObject



54
55
56
57
58
59
60
61
62
# File 'app/controllers/concerns/api/customfields.rb', line 54

def create_custom_data
  @customfield = Customfield.find(params[:field_id])
  if @customfield.nil?
    render json: { data: 'Customfield ' + params[:field_id] + ' not found' }, status: :not_found
  else
    @customfield.customdatas.create(user_id: current_user.id, value: validate_data[:value])
    render json: { data: 'Data entered' }, status: :created
  end
end

#create_custom_fieldObject



12
13
14
15
16
17
18
19
# File 'app/controllers/concerns/api/customfields.rb', line 12

def create_custom_field
  field = @obj.customfields.create(validate_fields)
  if field.nil?
    render json: { data: 'an error as occured' }, status: :unprocessable_entity
  else
    render json: { data: 'Fields created' }, status: :created
  end
end

#delete_custom_fieldObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/concerns/api/customfields.rb', line 34

def delete_custom_field
  @field = Customfield.find(params[:field_id])
  if @field.nil?
    render json: { data: 'Missing field id to update' }, status: :unprocessable_entity
  else
    # Delete the data associated first
    @field.customdatas.all.map(&:delete)
    # Then delete the field itself
    if @field.delete
      render json: { data: 'Fields deleted' }, status: :ok
    else
      render json: { data: 'an error as occured' }, status: :unprocessable_entity
    end
  end
end

#get_custom_dataObject



82
83
84
85
# File 'app/controllers/concerns/api/customfields.rb', line 82

def get_custom_data
  @customfield = Customfield.find(params[:field_id])
  render json: @customfield.customdatas, each_serializer: Api::CustomdataSerializer
end

#get_custom_fieldsObject



50
51
52
# File 'app/controllers/concerns/api/customfields.rb', line 50

def get_custom_fields
  render json: @obj.customfields, each_serializer: Api::CustomfieldSerializer
end

#get_my_custom_datasObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/concerns/api/customfields.rb', line 87

def get_my_custom_datas
  answers = []
  if current_user.nil?
    render json: { data: 'Forbidden' }, status: :forbidden
  else
    @obj.customfields.map do |customfield|
      answer = customfield.customdatas.find_by(user_id: current_user.id)
      answers << answer unless answer.nil?
    end
    render json: answers, each_serializer: Api::CustomdataSerializer
  end
end

#update_custom_dataObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/concerns/api/customfields.rb', line 64

def update_custom_data
  @customfield = Customfield.find(params[:field_id])
  if @customfield.nil?
    render json: { data: 'Customfield ' + params[:field_id] + ' not found' }, status: :not_found
  else
    @customdata = @customfield.customdatas.find_by(user_id: current_user.id)
    if @customdata.nil?
      render json: { data: 'Customdata not found' }, status: :not_found
    else
      if @customdata.update(validate_data)
        render json: { data: 'Data updated' }, status: :ok
      else
        render json: { data: 'an error as occured' }, status: :unprocessable_entity
      end
    end
  end
end

#update_custom_fieldObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/concerns/api/customfields.rb', line 21

def update_custom_field
  @field = Customfield.find(params[:field_id])
  if @field.nil?
    render json: { data: 'Field ot found' }, status: :not_found
  else
    if @field.update(validate_fields)
      render json: { data: 'Fields updated' }, status: :ok
    else
      render json: { data: 'an error as occured' }, status: :unprocessable_entity
    end
  end
end

#validate_dataObject



104
105
106
# File 'app/controllers/concerns/api/customfields.rb', line 104

def validate_data
  params.require(:data).permit(:value)
end

#validate_fieldsObject



100
101
102
# File 'app/controllers/concerns/api/customfields.rb', line 100

def validate_fields
  params.require(:fields).permit(:description, :name, :field_type, :optional)
end