Skip to content

Commit

Permalink
Move table default_order from Redis to PG
Browse files Browse the repository at this point in the history
  • Loading branch information
isc committed Oct 23, 2023
1 parent 2562b49 commit cd5da6b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ GEM
crass (1.0.6)
database_cleaner (1.8.5)
date (3.3.3)
debug_inspector (1.0.0)
debug_inspector (1.1.0)
docile (1.4.0)
dry-cli (1.0.0)
encryptor (3.0.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def update
end
table_configuration.update! table_configuration_params if table_configuration.present?
resource.per_page = params[:per_page] if params[:per_page]
resource.default_order = params[:default_order].join(' ') if params[:default_order].present?
resource.save
redirect_back fallback_location: resources_path(resource.table), success: 'Settings successfully saved'
end
Expand Down Expand Up @@ -56,6 +55,7 @@ def table_configuration_params
end
res[:label_column] = params[:label_column].presence if params[:label_column]
res[:validations] = params[:validations].delete_if(&:empty?) if params[:validations]
res[:default_order] = params[:default_order].join(' ').strip if params[:default_order].present?
res
end
end
17 changes: 11 additions & 6 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Resource
VALIDATES_UNIQUENESS_OF = 'validates_uniqueness_of'.freeze
VALIDATORS = [VALIDATES_PRESENCE_OF, VALIDATES_UNIQUENESS_OF].freeze

attr_accessor :default_order, :enum_values, :table, :datas
attr_accessor :enum_values, :table, :datas
delegate :validations, :export_col_sep, :export_skip_header, to: :table_configuration

def initialize generic, table
Expand All @@ -19,13 +19,9 @@ def load
@datas = JSON.parse(value).symbolize_keys!
@columns = datas[:columns].symbolize_keys!
@column = datas[:column] || {}
if datas[:default_order].present? && column_names.include?(datas[:default_order].to_s.split(' ').first.to_sym)
@default_order = datas[:default_order]
end
@per_page = datas[:per_page] || @generic.account.per_page
@enum_values = datas[:enum_values] || []
end
@default_order ||= default_primary_keys_order
set_missing_columns_conf
end

Expand All @@ -37,6 +33,15 @@ def default_primary_keys_order
primary_keys.map {|key| "#{key} desc"}.join ',' if primary_keys.any?
end

def default_order
return @default_order if @default_order
@default_order = table_configuration.default_order
if @default_order.present? && column_names.exclude?(default_order.split(' ').first.to_sym)
@default_order = nil
end
@default_order ||= default_primary_keys_order
end

def default_order_column
default_order&.split(' ')&.first
end
Expand Down Expand Up @@ -104,7 +109,7 @@ def column_names
end

def save
settings = { columns: @columns, column: @column, default_order: @default_order, enum_values: @enum_values }
settings = { columns: @columns, column: @column, enum_values: @enum_values }
settings[:per_page] = @per_page if @generic.account.per_page != @per_page
REDIS.set settings_key, settings.to_json
table_configuration.save!
Expand Down
2 changes: 1 addition & 1 deletion app/views/resources/panels/_misc.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
options_from_collection_for_select(resource.schema, :first, :first, resource.default_order_column), class: 'select2 form-control'
.col-sm-3
= label_tag 'default_order__asc', class: 'radio-inline' do
= radio_button_tag 'default_order[]', 'asc', (resource.default_order_direction == 'asc')
= radio_button_tag 'default_order[]', '', (resource.default_order_direction == 'asc')
' asc
= label_tag 'default_order__desc', class: 'radio-inline' do
= radio_button_tag 'default_order[]', 'desc', (resource.default_order_direction == 'desc')
Expand Down
38 changes: 19 additions & 19 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_10_17_184959) do

ActiveRecord::Schema[7.0].define(version: 2023_10_23_091541) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "accounts", force: :cascade do |t|
t.string "name"
t.text "encrypted_db_url"
t.string "adapter"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "last_tip_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.datetime "last_tip_at", precision: nil
t.string "last_tip_identifier"
t.boolean "tips_opt_in", default: true
t.string "application_time_zone", default: "UTC", null: false
Expand All @@ -36,8 +35,8 @@
create_table "collaborators", force: :cascade do |t|
t.integer "user_id"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.boolean "is_administrator", default: false, null: false
t.string "email"
t.string "token", null: false
Expand All @@ -55,8 +54,8 @@
t.string "external_id"
t.string "public_key"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "sign_count", default: 0, null: false
t.index ["external_id"], name: "index_credentials_on_external_id", unique: true
t.index ["user_id"], name: "index_credentials_on_user_id"
Expand All @@ -66,38 +65,39 @@
t.string "name"
t.integer "account_id"
t.text "permissions"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "searches", force: :cascade do |t|
t.string "name"
t.string "table"
t.integer "account_id"
t.jsonb "conditions"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["account_id"], name: "index_searches_on_account_id"
end

create_table "table_configurations", force: :cascade do |t|
t.integer "account_id"
t.string "table"
t.jsonb "polymorphic_associations", default: []
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.jsonb "validations", default: [], null: false
t.string "label_column"
t.string "export_col_sep"
t.boolean "export_skip_header"
t.string "default_order"
t.index ["account_id"], name: "index_table_configurations_on_account_id"
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.string "webauthn_id"
t.index ["webauthn_id"], name: "index_users_on_webauthn_id", unique: true
end
Expand All @@ -108,8 +108,8 @@
t.string "order"
t.string "columns"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.string "type"
t.string "grouping"
end
Expand Down

0 comments on commit cd5da6b

Please sign in to comment.