app/models/effective/effective_datatable/options.rb in effective_datatables-2.4.6 vs app/models/effective/effective_datatable/options.rb in effective_datatables-2.5.0

- old
+ new

@@ -6,16 +6,40 @@ def initialize_options @table_columns = initialize_column_options(@table_columns) end + def initialize_scopes + @scopes = initialize_scope_options(@scopes) + end + def quote_sql(name) collection_class.connection.quote_column_name(name) rescue name end protected + # The scope DSL is + # scope :start_date, default_value, options {} + + # A scope comes to us like {:start_date => {default: Time.zone.now, filter: {as: :select, collection: ... input_html :}}} + # We want to make sure an input_html: { value: default } exists + def initialize_scope_options(scopes) + (scopes || []).each do |name, options| + options[:filter] ||= HashWithIndifferentAccess.new() + options[:filter][:input_html] ||= HashWithIndifferentAccess.new() + options[:filter][:input_html][:value] = options[:default] + end + + # For each scope, copy it into the attributes, so we can get at the value + (scopes || []).each do |name, options| + self.attributes[name] ||= options[:default] + end + + scopes + end + def initialize_column_options(cols) sql_table = (collection.table rescue nil) # Here we identify all belongs_to associations and build up a Hash like: # {user: {foreign_key: 'user_id', klass: User}, order: {foreign_key: 'order_id', klass: Effective::Order}} @@ -79,19 +103,22 @@ :has_and_belongs_to_many elsif cols[name][:bulk_actions_column] :bulk_actions_column elsif name.include?('_address') && (collection_class.new rescue nil).respond_to?(:effective_addresses) :effective_address - elsif name == 'id' || name.include?('year') || name.include?('_id') - :non_formatted_integer elsif sql_column.try(:type).present? sql_column.type else :string # When in doubt end ) cols[name][:class] = "col-#{cols[name][:type]} col-#{name} #{cols[name][:class]}".strip + + # Formats + if name == 'id' || name.include?('year') || name.include?('_id') + cols[name][:format] = :non_formatted_integer + end # We can't really sort a HasMany or EffectiveAddress field if [:has_many, :effective_address].include?(cols[name][:type]) cols[name][:sortable] = false end