Skip to content

Commit

Permalink
Merge branch 'main' into version-2
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 19, 2024
2 parents a26aa1a + 0229b35 commit 89d456c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 89 deletions.
5 changes: 1 addition & 4 deletions .document
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt
lib/**/*.rb
2 changes: 2 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--markup=markdown
--readme=README.md
9 changes: 9 additions & 0 deletions lib/datagrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ def self.included(base)
end
end

def self.configuration
@configuration ||= Configuration.new
end

# Configure
def self.configure(&block)
block.call(configuration)
end

class ConfigurationError < StandardError; end
class ArgumentError < ::ArgumentError; end
class ColumnUnavailableError < StandardError; end
Expand Down
26 changes: 13 additions & 13 deletions lib/datagrid/columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ module Datagrid
# # ]
# }
#
# == Column Value
# ## Column Value
#
# The value of a column can be defined by passing a block to `Datagrid.column`.
#
# === Basic Column Value
# ### Basic Column Value
#
# If no block is provided, the column value is generated automatically by sending the column name method to the model.
#
Expand All @@ -57,7 +57,7 @@ module Datagrid
#
# column(:completed) { |asset| asset.completed? }
#
# === Advanced Column Value
# ### Advanced Column Value
#
# You can also pass the Datagrid object itself to define more complex column values.
#
Expand All @@ -83,14 +83,14 @@ module Datagrid
# row.total_sales / row.number_of_sales
# end
#
# == Using Database Expressions
# ## Using Database Expressions
#
# Columns can use database expressions to directly manipulate data in the database.
#
# column(:count_of_users, 'count(user_id)')
# column(:uppercase_name, 'upper(name)')
#
# == HTML Columns
# ## HTML Columns
#
# Columns can have different formats for HTML and non-HTML representations.
#
Expand All @@ -100,13 +100,13 @@ module Datagrid
# end
# end
#
# == Column Value Cache
# ## Column Value Cache
#
# Enables grid-level caching for column values.
#
# self.cached = true
#
# == Ordering
# ## Ordering
#
# Columns can specify SQL ordering expressions using the `:order` and `:order_desc` options.
#
Expand Down Expand Up @@ -148,19 +148,19 @@ module Datagrid
# Time.at(model.finished_at - model.accepted_at).strftime("%H:%M:%S")
# end
#
# == Default Column Options
# ## Default Column Options
#
# Default options for all columns in a grid can be set using `default_column_options`.
#
# self.default_column_options = { order: false }
#
# == Columns Visibility
# ## Columns Visibility
#
# Columns can be dynamically shown or hidden based on the grid's `column_names` accessor.
#
# grid.column_names = [:id, :name]
#
# == Dynamic Columns
# ## Dynamic Columns
#
# Columns can be defined dynamically on a grid instance or based on data.
#
Expand All @@ -170,13 +170,13 @@ module Datagrid
# model.extra_data
# end
#
# == Localization
# ## Localization
#
# Column headers can be localized using the `:header` option or through i18n files.
#
# column(:active, header: Proc.new { I18n.t("activated") })
#
# == Preloading Associations
# ## Preloading Associations
#
# Preload database associations for better performance.
#
Expand All @@ -190,7 +190,7 @@ module Datagrid
#
# column(:account_name, preload: { |s| s.includes(:account) })
#
# == Decorator
# ## Decorator
#
# A decorator or presenter class can be used around each object in the `scope`.
#
Expand Down
34 changes: 13 additions & 21 deletions lib/datagrid/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
# frozen_string_literal: true

module Datagrid
def self.configuration
@configuration ||= Configuration.new
end

def self.configure
yield(configuration)
end

# == Configuration
# ## Configuration
#
# Datagrid provides several configuration options.
#
# Here is the API reference and a description of the available options:
#
# Datagrid.configure do |config|
#
# # Defines date formats that can be used to parse dates.
# # Note: Multiple formats can be specified. The first format is used to format dates as strings,
# # while other formats are used only for parsing dates from strings (e.g., if your app supports multiple formats).
# config.date_formats = ["%m/%d/%Y", "%Y-%m-%d"]
#
# # Defines timestamp formats that can be used to parse timestamps.
# # Note: Multiple formats can be specified. The first format is used to format timestamps as strings,
# # while other formats are used only for parsing timestamps from strings (e.g., if your app supports multiple formats).
# config.datetime_formats = ["%m/%d/%Y %h:%M", "%Y-%m-%d %h:%M:%s"]
# ``` ruby
# Datagrid.configure do |config|
# # Defines date formats that can be used to parse dates.
# # Note: Multiple formats can be specified. The first format is used to format dates as strings,
# # while other formats are used only for parsing dates from strings (e.g., if your app supports multiple formats).
# config.date_formats = ["%m/%d/%Y", "%Y-%m-%d"]
#
# end
# # Defines timestamp formats that can be used to parse timestamps.
# # Note: Multiple formats can be specified. The first format is used to format timestamps as strings,
# # while other formats are used only for parsing timestamps from strings (e.g., if your app supports multiple formats).
# config.datetime_formats = ["%m/%d/%Y %h:%M", "%Y-%m-%d %h:%M:%s"]
# end
# ```
#
# These options can be set globally in your application to customize Datagrid’s behavior.
class Configuration
Expand Down
26 changes: 13 additions & 13 deletions lib/datagrid/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Datagrid
# grid = UserGrid.new(posts_count: 1, name: "John")
# grid.assets # SELECT * FROM users WHERE users.posts_count > 1 AND name = 'John'
#
# = Filter Block
# # Filter Block
#
# Filter blocks should always return a chainable ORM object (e.g., `ActiveRecord::Relation`) rather than an `Array`.
#
Expand All @@ -39,7 +39,7 @@ module Datagrid
# scope.where("name #{grid.predicate} ?", "%#{value}%")
# end
#
# = Filter Types
# # Filter Types
#
# Filters perform automatic type conversion. Supported filter types include:
#
Expand All @@ -54,52 +54,52 @@ module Datagrid
# - `string`
# - `dynamic`
#
# == Default
# ## Default
#
# `:default` - Leaves the value as is.
#
# == Date
# ## Date
#
# `:date` - Converts value to a date. Supports the `:range` option to accept date ranges.
#
# filter(:created_at, :date, range: true, default: proc { 1.month.ago.to_date..Date.today })
#
# == Datetime
# ## Datetime
#
# `:datetime` - Converts value to a timestamp. Supports the `:range` option to accept time ranges.
#
# filter(:created_at, :datetime, range: true, default: proc { 1.hour.ago..Time.now })
#
# == Enum
# ## Enum
#
# `:enum` - For collection selection with options like `:select` and `:multiple`.
#
# filter(:user_type, :enum, select: ['Admin', 'Customer', 'Manager'])
# filter(:category_id, :enum, select: proc { Category.all.map { |c| [c.name, c.id] } }, multiple: true)
#
# == Boolean
# ## Boolean
#
# `:boolean` - Converts value to `true` or `false`.
#
# == Xboolean
# ## Xboolean
#
# `:xboolean` - Subtype of `enum` filter that provides "Yes", "No", and "Any" options.
#
# filter(:active, :xboolean)
#
# == Integer
# ## Integer
#
# `:integer` - Converts value to an integer. Supports the `:range` option.
#
# filter(:posts_count, :integer, range: true, default: (1..nil))
#
# == String
# ## String
#
# `:string` - Converts value to a string.
#
# filter(:email, :string)
#
# == Dynamic
# ## Dynamic
#
# Provides a builder for dynamic SQL conditions.
#
Expand All @@ -108,7 +108,7 @@ module Datagrid
# UsersGrid.new(condition1: [:name, "=~", "John"], condition2: [:posts_count, ">=", 1])
# UsersGrid.assets # SELECT * FROM users WHERE name like '%John%' and posts_count >= 1
#
# = Filter Options
# # Filter Options
#
# Options that can be passed to any filter:
#
Expand All @@ -123,7 +123,7 @@ module Datagrid
# filter(:id, :integer, header: "Identifier")
# filter(:created_at, :date, range: true, default: proc { 1.month.ago.to_date..Date.today })
#
# = Localization
# # Localization
#
# Filter labels can be localized or specified via the `:header` option:
#
Expand Down
Loading

0 comments on commit 89d456c

Please sign in to comment.