Class: Simpleadmin::Adapters::Postgres

Inherits:
Base
  • Object
show all
Defined in:
lib/simpleadmin/adapters/postgres.rb

Overview

Postgres adapter for API endpoints

Since:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Simpleadmin::Adapters::Base

Instance Method Details

#create_resource(table_name, resource_params) ⇒ Object

Since:

  • 1.0.0



55
56
57
# File 'lib/simpleadmin/adapters/postgres.rb', line 55

def create_resource(table_name, resource_params)
  Config.on_create.call(model_class_by_table_name(table_name), resource_params)
end

#destroy_resouce(table_name, resource_id, _primary_key = :id) ⇒ Object

Since:

  • 1.0.0



63
64
65
# File 'lib/simpleadmin/adapters/postgres.rb', line 63

def destroy_resouce(table_name, resource_id, _primary_key=:id)
  Config.on_destroy.call(model_class_by_table_name(table_name), resource_id)
end

#quantity(table_name) ⇒ Object

Since:

  • 1.0.0



67
68
69
70
71
72
# File 'lib/simpleadmin/adapters/postgres.rb', line 67

def quantity(table_name)
  {
    widget_name: :quantity,
    result: client.from(table_name).count
  }.to_json
end

#resource(resource_id, table_name, table_fields) ⇒ Object

Since:

  • 1.0.0



51
52
53
# File 'lib/simpleadmin/adapters/postgres.rb', line 51

def resource(resource_id, table_name, table_fields)
  client.from(table_name).first(id: resource_id).slice(*table_fields.map(&:to_sym)).to_json
end

#resources(permitted_params_values) ⇒ Object

Since:

  • 1.0.0



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
# File 'lib/simpleadmin/adapters/postgres.rb', line 24

def resources(permitted_params_values)
  table_name, table_fields, per_page, page, query, model_attributes, sort = *permitted_params_values

  per_page = per_page.to_i
  page = page.to_i

  table_fields_names = table_fields.map { |field| field['field_name'].to_sym }

  total = client.from(table_name).count
  resources = client.from(table_name).limit(per_page)

  resources, total = *search(query, table_name, model_attributes) unless query.nil? || query.empty?

  resources = resources.offset((per_page * page) - per_page).select(*table_fields_names)

  if order_asc?(sort['order'])
    resources = resources.order(Sequel.asc(sort['column_name'].to_sym))
  elsif order_desc?(sort['order'])
    resources = resources.order(Sequel.desc(sort['column_name'].to_sym))
  end

  {
    resources: Decorators::FieldDecorator.call(resources, table_name, table_fields),
    total: total
  }.to_json
end

#table_columns(table_name) ⇒ Object

Since:

  • 1.0.0



20
21
22
# File 'lib/simpleadmin/adapters/postgres.rb', line 20

def table_columns(table_name)
  columns_by_table_name(table_name).to_json
end

#tablesObject

Since:

  • 1.0.0



11
12
13
14
15
16
17
18
# File 'lib/simpleadmin/adapters/postgres.rb', line 11

def tables
  table_names.map do |table_name|
    {
      table_name: table_name,
      table_columns: columns_by_table_name(table_name)
    }
  end.to_json
end

#update_resource(table_name, resource_id, resource_params, _primary_key = :id) ⇒ Object

Since:

  • 1.0.0



59
60
61
# File 'lib/simpleadmin/adapters/postgres.rb', line 59

def update_resource(table_name, resource_id, resource_params, _primary_key=:id)
  Config.on_update.call(model_class_by_table_name(table_name), resource_id, resource_params)
end

#week_statistic(table_name) ⇒ Object

Since:

  • 1.0.0



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/simpleadmin/adapters/postgres.rb', line 74

def week_statistic(table_name)
  result = 6.downto(0).map do |day|
    current_date_time = DateTime.now - day

    client.from(table_name).where(
      created_at: beggining_of_day(current_date_time)..at_end_of_day(current_date_time)
    ).count
  end

  {
    widget_name: :week_statistic,
    result: result
  }.to_json
end