Class: Zendesk2::CreateView

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/zendesk2/create_view.rb

Instance Attribute Summary

Attributes included from Request

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#call, cistern_included, #data, #delete!, #error!, #find!, #html_url_for, included, #mock_response, #page, #page_params!, #page_params?, #pluralize, #real, #real_request, #request_body, #request_params, #request_path, #resources, #response, #timestamp, #url_for

Class Method Details

.accepted_attributesObject



9
10
11
# File 'lib/zendesk2/create_view.rb', line 9

def self.accepted_attributes
  %w(title all any active output restriction)
end

.view_columnsObject



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
# File 'lib/zendesk2/create_view.rb', line 13

def self.view_columns
  @_view_columns ||= {
    'assigned'           => 'Assigned date',
    'assignee'           => 'Assignee',
    'brand'              => 'Brand',
    'created'            => 'Request date',
    'description'        => 'Subject',
    'due_date'           => 'Due Date',
    'group'              => 'Group',
    'locale_id'          => 'Requester language',
    'nice_id'            => 'ID',
    'organization'       => 'Organization',
    'priority'           => 'Priority',
    'requester'          => 'Requester',
    'satisfaction_score' => 'Satisfaction',
    'score'              => 'Score',
    'solved'             => 'Solved date',
    'status'             => 'Status',
    'submitter'          => 'Submitter',
    'ticket_form'        => 'Ticket form',
    'type'               => 'Ticket type',
    'updated'            => 'Latest update',
    'updated_assignee'   => 'Latest update by assignee',
    'updated_by_type'    => 'Latest updater type (agent/end-user)',
    'updated_requester'  => 'Latest update by requester',
  }.freeze
end

Instance Method Details

#mockObject



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
# File 'lib/zendesk2/create_view.rb', line 45

def mock
  create_params = view_params

  if Zendesk2.blank?(create_params['title'])
    error!(:invalid, details: { 'base' => [{ 'title' => 'Title: cannot be blank' }] })
  end

  if Array(create_params['any']).empty? && Array(create_params['all']).empty?
    error!(:invalid, details: { 'base' => ['Invalid conditions: You must select at least one condition'] })
  end

  identity = cistern.serial_id

  output = view_params.delete('output') || {}
  columns = (output['columns'] || []).inject([]) do |a, e|
    a << { 'id' => e, 'name' => self.class.view_columns.fetch(e) }
  end

  record = {
    'id'               => identity,
    'url'              => url_for("/views/#{identity}.json"),
    'created_at'       => timestamp,
    'updated_at'       => timestamp,
    'active'           => true,
    'execution'        => {
      'columns'       => columns,
      'fields'        => columns,
      'custom_fields' => [],
      'group_by'      => create_params['group_by'],
      'group_order'   => create_params['group_order'],
      'sort_by'       => create_params['sort_by'],
      'sort_order'    => create_params['sort_order'],
      'sort' => {
        'id'    => create_params['sort_by'],
        'order' => create_params['sort_order'],
        'title' => (create_params['sort_by'] && create_params['sort_by'].to_s.upcase),
      },
      'group' => {
        'id'    => create_params['group_by'],
        'order' => create_params['group_order'],
        'title' => (create_params['group_by'] && create_params['group_by'].to_s.upcase),
      },
    },
    'conditions' => {
      'any' => Array(create_params['any']),
      'all' => Array(create_params['all']),
    },
  }.merge(create_params)

  cistern.data[:views][identity] = record

  mock_response('view' => record)
end

#view_paramsObject



41
42
43
# File 'lib/zendesk2/create_view.rb', line 41

def view_params
  Cistern::Hash.slice(params.fetch('view'), *self.class.accepted_attributes)
end