class Simpleadmin::Adapters::Postgres
Postgres
adapter for API endpoints
@since 1.0.0
Public Instance Methods
create_resource(table_name, resource_params)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 20 def table_columns(table_name) columns_by_table_name(table_name).to_json end
tables()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
at_end_of_day(date)
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 99 def at_end_of_day(date) DateTime.new(date.year, date.month, date.day, 23, 59, 59, 59) end
beggining_of_day(date)
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 95 def beggining_of_day(date) DateTime.new(date.year, date.month, date.day, 0, 0, 0, 0) end
client()
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 91 def client @client ||= Sequel.connect(database_credentials) end
columns_by_table_name(name)
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 103 def columns_by_table_name(name) client.schema(name).map do |column_attributes| column_name, column_information = *column_attributes { column_name: column_name, data_type: column_information[:db_type] } end end
search(search_query, table_name, model_attributes)
click to toggle source
# File lib/simpleadmin/adapters/postgres.rb, line 114 def search(search_query, table_name, model_attributes) query_expressions = model_attributes.map do |model_attribute| Sequel.like(model_attribute.to_sym, "%#{search_query}%") end query = query_expressions.first if query_expressions.one? query_expressions.each_cons(2) do |current_value, next_value| query = query.nil? ? current_value | next_value : query | (current_value | next_value) end [ client.from(table_name).where(query), client.from(table_name).where(query).count ] end