aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers
diff options
context:
space:
mode:
authorTeddy Wing2017-06-12 14:34:46 +0200
committerTeddy Wing2017-06-12 14:34:46 +0200
commite89aead78b6182a0789d53e46b230df54aee82f3 (patch)
treef663a160e1ed68507ad8a1f962d335431c9fe6a6 /app/helpers
parent91f6bc0b56d4b51645470c9f470d874f92d38b50 (diff)
downloadchouette-core-e89aead78b6182a0789d53e46b230df54aee82f3.tar.bz2
TableBuilder: Support a column-level sortable flag
Provides an option to set a column as non-sortable when defining the column in the `table_builder` call. Not written well, but it gets the idea across. We allow column hash values to be hashes themselves (while continuing to allow them to be `Proc`s and strings, this mixing of types is what I really don't like). In that column-level hash, we can pass `sortable: false` to disable sorting on that column. By default, sorting is turned on for all columns. Change the method signature of `sortable_columns`. Rename it to `build_column_header`, as it will now generate a correct column header regardless of whether the column is supposed to be sortable or not. I moved some dependencies outside of the method, like `collection` (since we only need the model from the collection), and the sort fields on `params`. Unfortunately wasn't able to completely extricate `params` for this method. Left the `params.merge` call that builds a link in this method. Ideally I'd like that removed so that the method no longer depends on global variables. In `build_column_header`, if the column is not sortable, we return the label defined by the key in the columns hash defined by the `table_builder` caller. Otherwise, we build some HTML that includes links on a couple of arrow buttons to enable sorting. In `#thead`, we check to see if the columns hash value has a `sortable` defined, in which case that flag gets used to determine what the column header will be. I renamed the block variables here to be more descriptive. Still not a huge fan of `attribute` as a name, though, because it can now also be a Hash. Another aspect of this change that I don't like. In `#tbody`, needed to add a few extra lines to check whether `attribute` is a Hash and get the real attribute if so. Really don't like that either because it doesn't look good. Update the sortable spec to pass a hash as the value of the column that should not be sorted. It defines a `:sortable` key that tells the builder not to allow this column to be sorted. Refs #3479
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/table_builder_helper.rb36
1 files changed, 27 insertions, 9 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb
index b3b5e3cde..954ec5314 100644
--- a/app/helpers/table_builder_helper.rb
+++ b/app/helpers/table_builder_helper.rb
@@ -35,13 +35,19 @@ module TableBuilderHelper
hcont << content_tag(:th, checkbox(id_name: '0', value: 'all'))
end
- columns.map do |k, v|
- # These columns are hard-coded to not be sortable
- if ["ID Codif", "Oid", "OiD", "ID Reflex", "Arrêt de départ", "Arrêt d'arrivée", "Période de validité englobante", "Période englobante", "Nombre de courses associées", "Journées d'application"].include? k
- hcont << content_tag(:th, k)
- else
- hcont << content_tag(:th, sortable_columns(collection, k))
+ columns.map do |key, attribute|
+ sortable = true
+ if attribute.is_a?(Hash) && attribute.has_key?(:sortable)
+ sortable = attribute[:sortable]
end
+
+ hcont << content_tag(:th, build_column_header(
+ collection.model,
+ key,
+ sortable,
+ params[:sort],
+ params[:direction]
+ ))
end
# Inserts a blank column for the gear menu
hcont << content_tag(:th, '') if has_links
@@ -67,6 +73,10 @@ module TableBuilderHelper
end
columns.map do |k, attribute|
+ if attribute.is_a?(Hash)
+ attribute = attribute[:attribute]
+ end
+
value =
if Proc === attribute
attribute.call(item)
@@ -153,12 +163,20 @@ module TableBuilderHelper
end
# TODO: clean up?
- def sortable_columns collection, key
+ def build_column_header(
+ collection_model,
+ key,
+ sortable,
+ sort_on,
+ sort_direction
+ )
# #<ActiveRecord::Relation [#<Referential id: 4, name: "Referential Yanis Gaillard", slug: "referential_yanis_gaillard", created_at: "2017-05-02 12:37:38", updated_at: "2017-05-02 12:37:49", prefix: "c4nqg22nvt", projection_type: nil, time_zone: "Paris", bounds: nil, organisation_id: 1, geographical_bounds: nil, user_id: nil, user_name: nil, data_format: "neptune", line_referential_id: 1, stop_area_referential_id: 1, workbench_id: 1, archived_at: nil, created_from_id: nil, ready: true>, #<Referential id: 3, name: "Test Referential 2017.04.25", slug: "test_referential_20170425", created_at: "2017-04-25 10:08:49", updated_at: "2017-04-25 10:08:51", prefix: "test_referential_20170425", projection_type: "", time_zone: "Paris", bounds: "SRID=4326;POLYGON((-5.2 42.25,-5.2 51.1,8.23 51.1,...", organisation_id: 1, geographical_bounds: nil, user_id: 1, user_name: "Wing Teddy", data_format: "neptune", line_referential_id: 1, stop_area_referential_id: 1, workbench_id: 1, archived_at: nil, created_from_id: nil, ready: true>]>
# (byebug) collection.model
# Referential(id: integer, name: string, slug: string, created_at: datetime, updated_at: datetime, prefix: string, projection_type: string, time_zone: string, bounds: string, organisation_id: integer, geographical_bounds: text, user_id: integer, user_name: string, data_format: string, line_referential_id: integer, stop_area_referential_id: integer, workbench_id: integer, archived_at: datetime, created_from_id: integer, ready: boolean)
# params = {"controller"=>"workbenches", "action"=>"show", "id"=>"1", "q"=>{"archived_at_not_null"=>"1", "archived_at_null"=>"1"}}
- direction = (key.to_s == params[:sort] && params[:direction] == 'desc') ? 'asc' : 'desc'
+ return key if !sortable
+
+ direction = (key.to_s == sort_on && sort_direction == 'desc') ? 'asc' : 'desc'
link_to(params.merge({direction: direction, sort: key})) do
pic1 = content_tag :span, '', class: "fa fa-sort-asc #{(direction == 'desc') ? 'active' : ''}"
@@ -168,7 +186,7 @@ module TableBuilderHelper
# TODO: figure out a way to maybe explicitise the dynamicness of getting the model type from the `collection`.
# TODO: rename `pics` to something like `icons` or arrow icons or some such
- (column_header_label(collection.model, key) + pics).html_safe
+ (column_header_label(collection_model, key) + pics).html_safe
end
end