Class: Api::V1::TrackedStatusTypesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/tracked_status_types_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/api/v1/tracked_status_types_controller.rb', line 126

def create
  description = params[:description].strip

  ActiveRecord::Base.transaction do
    tracked_status_type = TrackedStatusType.create(description: description, internal_identifier: description.to_iid)

    if params[:parent] != 'No Parent'
      parent = TrackedStatusType.iid(params[:parent])
      tracked_status_type.move_to_child_of(parent)
    elsif params[:default_parent]
      parent = TrackedStatusType.iid(params[:default_parent])
      tracked_status_type.move_to_child_of(parent)
    end

    render :json => {success: true, tracked_status_type: tracked_status_type.to_data_hash}
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/api/v1/tracked_status_types_controller.rb', line 5

def index
  # look up parent by internal identifier
  if params[:parent].present?
    parent = nil
    # create parent if it doesn't exist
    # if the parent param is a comma separated string then
    # the parent is nested from left to right
    params[:parent].split(',').each do |parent_iid|
      if parent
        parent = TrackedStatusType.find_or_create(parent_iid, parent_iid.humanize, parent)
      else
        parent = TrackedStatusType.find_or_create(parent_iid, parent_iid.humanize)
      end
    end

    respond_to do |format|
      format.tree do
        render :json => {success: true, tracked_status_types: parent.children_to_tree_hash}
      end
      format.json do
        render :json => {success: true, tracked_status_types: parent.children.all.collect{|item| item.to_data_hash}}
      end
    end

    # if parent id is passed find parent and get its children
  elsif params[:parent_id].present?
    parent = TrackedStatusType.find(params[:parent_id])

    respond_to do |format|
      format.tree do
        render :json => {success: true, tracked_status_types: parent.children_to_tree_hash}
      end
      format.json do
        render :json => {success: true, tracked_status_types: parent.children.all.collect{|item| item.to_data_hash}}
      end
    end
    # if ids are passed look up on the Tracked Status Types with the ids passed
  elsif params[:ids].present?
    ids = params[:ids].split(',').compact

    tracked_status_types = []

    ids.each do |id|
      # check if id is a integer if so fine by id
      if id.is_integer?
        tracked_status_type = TrackedStatusType.find(id)
      else
        tracked_status_type = TrackedStatusType.iid(id)
      end

      respond_to do |format|
        format.tree do
          data = tracked_status_type.to_hash({
                                       only: [:id, :parent_id, :internal_identifier],
                                       leaf: tracked_status_type.leaf?,
                                       text: tracked_status_type.to_label,
                                       children: []
                                   })

          parent = nil
          tracked_status_types.each do |tracked_status_type_hash|
            if tracked_status_type_hash[:id] == data[:parent_id]
              parent = tracked_status_type_hash
            end
          end

          if parent
            parent[:children].push(data)
          else
            tracked_status_types.push(data)
          end
        end
        format.json do
          tracked_status_types.push(tracked_status_type.to_data_hash)
        end
      end

    end

    render :json => {success: true, tracked_status_types: role_types}

    # get all role types
  else
    respond_to do |format|
      format.tree do
        nodes = [].tap do |nodes|
          TrackedStatusType.roots.each do |root|
            nodes.push(root.to_tree_hash)
          end
        end

        render :json => {success: true, tracked_status_types: nodes}
      end
      format.json do
        render :json => {success: true, tracked_status_types: TrackedStatusType.where('parent_id is null').all.collect{|item| item.to_data_hash}}
      end
    end

  end
end

#showObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/api/v1/tracked_status_types_controller.rb', line 106

def show
  id = params[:id]

  # check if id is a integer if so fine by id
  if id.is_integer?
    tracked_status_type = TrackedStatusType.find(id)
  else
    tracked_status_type = TrackedStatusType.iid(id)
  end

  respond_to do |format|
    format.tree do
      render :json => {success: true, tracked_status_type: tracked_status_type.to_tree_hash}
    end
    format.json do
      render :json => {success: true, tracked_status_type: tracked_status_type.to_data_hash}
    end
  end
end