Class: Kaui::CustomFieldsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/custom_fields_controller.rb

Constant Summary

Constants included from EngineControllerUtil

EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#check_object_existObject



46
47
48
49
50
51
# File 'app/controllers/kaui/custom_fields_controller.rb', line 46

def check_object_exist
  param_uuid = params[:uuid]
  param_object_type = params[:object_type]

  _check_object_exist(param_uuid, param_object_type)
end

#createObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/kaui/custom_fields_controller.rb', line 53

def create
  @custom_field = Kaui::CustomField.new(params.require(:custom_field))

  param_uuid = @custom_field.object_id

  test_uuid = nil
  case @custom_field.object_type.to_sym
  when :ACCOUNT
    begin
      test_uuid = Kaui::Account.find_by_id_or_key(param_uuid, false, false, options_for_klient)
    rescue StandardError
      # Ignore
    end
  when :BUNDLE
    begin
      test_uuid = Kaui::Bundle.find_by_id_or_key(param_uuid, options_for_klient)
    rescue StandardError
      # Ignore
    end
  when :SUBSCRIPTION
    begin
      test_uuid = Kaui::Subscription.find_by_id(param_uuid, options_for_klient)
    rescue StandardError
      # Ignore
    end
  when :INVOICE
    begin
      test_uuid = Kaui::Invoice.find_by_id(param_uuid, false, 'NONE', options_for_klient)
    rescue StandardError
      # Ignore
    end
  when :PAYMENT
    begin
      test_uuid = Kaui::InvoicePayment.find_by_id(param_uuid, false, true, options_for_klient)
    rescue StandardError
      # Ignore
    end
    begin
      test_uuid = Kaui::Payment.find_by_external_key(param_uuid, false, true, options_for_klient)
    rescue StandardError
      # Ignore
    end
  when :INVOICE_ITEM
    begin
      test_uuid = Kaui::Invoice.find_by_invoice_item_id(param_uuid, false, 'NONE', options_for_klient)
    rescue StandardError
      # Ignore
    end
  end

  if test_uuid.blank?
    flash[:error] = I18n.translate('object_invalid_dont_exist')
    redirect_to custom_fields_path
    return
  end

  model = case @custom_field.object_type.to_sym
          when :ACCOUNT
            Kaui::Account.new(account_id: @custom_field.object_id)
          when :BUNDLE
            Kaui::Bundle.new(bundle_id: @custom_field.object_id)
          when :SUBSCRIPTION
            Kaui::Subscription.new(subscription_id: @custom_field.object_id)
          when :INVOICE
            Kaui::Invoice.new(invoice_id: @custom_field.object_id)
          when :PAYMENT
            Kaui::Payment.new(payment_id: @custom_field.object_id)
          when :INVOICE_ITEM
            Kaui::InvoiceItem.new(invoice_item_id: @custom_field.object_id)
          else
            flash.now[:error] = I18n.translate('invalid_object_type', error: @custom_field.object_type)
            render :new and return
          end
  model.add_custom_field(@custom_field, current_user.kb_username, params[:reason], params[:comment], options_for_klient)

  redirect_to custom_fields_path, notice: I18n.translate('custom_field_created_success')
end

#indexObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/kaui/custom_fields_controller.rb', line 5

def index
  @search_query = params[:q]

  @ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
  @offset = params[:offset] || 0
  @limit = params[:limit] || 50

  @max_nb_records = @search_query.blank? ? Kaui::CustomField.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
end

#newObject



42
43
44
# File 'app/controllers/kaui/custom_fields_controller.rb', line 42

def new
  @custom_field = Kaui::CustomField.new
end

#paginationObject



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
# File 'app/controllers/kaui/custom_fields_controller.rb', line 15

def pagination
  searcher = lambda do |search_key, offset, limit|
    Kaui::CustomField.list_or_search(search_key, offset, limit, options_for_klient)
  end

  data_extractor = lambda do |custom_field, column|
    [
      custom_field.object_id,
      custom_field.object_type,
      custom_field.name,
      custom_field.value
    ][column]
  end

  formatter = lambda do |custom_field|
    url_for_object = view_context.url_for_object(custom_field.object_id, custom_field.object_type)
    [
      url_for_object ? view_context.link_to(custom_field.object_id, url_for_object) : custom_field.object_id,
      custom_field.object_type,
      custom_field.name,
      custom_field.value
    ]
  end

  paginate searcher, data_extractor, formatter
end