Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of ruby and erb syntax highlightings #1324

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions docs/docs/getting-started/simple-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mode should meet your needs.

## In your controller

```jsx
```ruby
def index
@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)
Expand All @@ -20,7 +20,7 @@ end
or without `distinct: true`, for sorting on an associated table's columns (in
this example, with preloading each Person's Articles and pagination):

```jsx
```ruby
def index
@q = Person.ransack(params[:q])
@people = @q.result.includes(:articles).page(params[:page])
Expand All @@ -35,7 +35,8 @@ Ransack uses a default `:q` param key for search params. This may be changed by
setting the `search_key` option in a Ransack initializer file (typically
`config/initializers/ransack.rb`):

```jsx
```ruby
puts "Hello, World"
deivid-rodriguez marked this conversation as resolved.
Show resolved Hide resolved
Ransack.configure do |c|
# Change default search parameter key name.
# Default key name is :q
Expand All @@ -48,7 +49,7 @@ end
After version 2.4.0 when searching a string query Ransack by default strips all whitespace around the query string.
This may be disabled by setting the `strip_whitespace` option in a Ransack initializer file:

```jsx
```ruby
Ransack.configure do |c|
# Change whitespace stripping behaviour.
# Default is true
Expand All @@ -66,7 +67,7 @@ which are defined in

Ransack's `search_form_for` helper replaces `form_for` for creating the view search form

```jsx
```erb
<%= search_form_for @q do |f| %>

# Search if the name field contains...
Expand Down Expand Up @@ -95,7 +96,7 @@ search predicates.

The `search_form_for` answer format can be set like this:

```jsx
```erb
<%= search_form_for(@q, format: :pdf) do |f| %>

<%= search_form_for(@q, format: :json) do |f| %>
Expand All @@ -105,7 +106,7 @@ The `search_form_for` answer format can be set like this:

Ransack's `sort_link` helper creates table headers that are sortable links

```jsx
```erb
<%= sort_link(@q, :name) %>
```
Additional options can be passed after the column parameter, like a different
Expand All @@ -114,13 +115,13 @@ column title or a default sort order.
If the first option after the column parameter is a String, it's considered a
custom label for the link:

```jsx
```erb
<%= sort_link(@q, :name, 'Last Name', default_order: :desc) %>
```

You can use a block if the link markup is hard to fit into the label parameter:

```jsx
```erb
<%= sort_link(@q, :name) do %>
<strong>Player Name</strong>
<% end %>
Expand All @@ -130,14 +131,14 @@ With a polymorphic association, you may need to specify the name of the link
explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue
[#421](https://github.com/activerecord-hackery/ransack/issues/421)):

```jsx
```erb
<%= sort_link(@q, :xxxable_of_Ymodel_type_some_attribute, 'Attribute Name') %>
```

If the first option after the column parameter and/or the label parameter is an
Array, it will be used for sorting on multiple fields:

```jsx
```erb
<%= sort_link(@q, :last_name, [:last_name, 'first_name asc'], 'Last Name') %>
```

Expand All @@ -148,7 +149,7 @@ Ransack to _always_ sort that particular field in the specified direction.
Multiple `default_order` fields may also be specified with a trailing options
Hash:

```jsx
```erb
<%= sort_link(@q, :last_name, %i(last_name first_name),
default_order: { last_name: 'asc', first_name: 'desc' }) %>
```
Expand All @@ -162,7 +163,7 @@ of a SQL function, you may do so using scopes. In your model, define scopes
whose names line up with the name of the virtual field you wish to sort by,
as so:

```jsx
```ruby
class Person < ActiveRecord::Base
scope :sort_by_reverse_name_asc, lambda { order("REVERSE(name) ASC") }
scope :sort_by_reverse_name_desc, lambda { order("REVERSE(name) DESC") }
Expand All @@ -171,7 +172,7 @@ class Person < ActiveRecord::Base

and you can then sort by this virtual field:

```jsx
```erb
<%= sort_link(@q, :reverse_name) %>
```

Expand All @@ -186,7 +187,7 @@ You can also enable a `default_arrow` which is displayed on all sortable fields
which are not currently used in the sorting. This is disabled by default so
nothing will be displayed:

```jsx
```ruby
Ransack.configure do |c|
c.custom_arrows = {
up_arrow: '<i class="custom-up-arrow-icon"></i>',
Expand All @@ -200,7 +201,7 @@ All sort links may be displayed without the order indicator
arrows by setting `hide_sort_order_indicators` to true in the initializer file.
Note that this hides the arrows even if they were customized:

```jsx
```ruby
Ransack.configure do |c|
c.hide_sort_order_indicators = true
end
Expand All @@ -209,7 +210,7 @@ end
Without setting it globally, individual sort links may be displayed without
the order indicator arrow by passing `hide_indicator: true` in the sort link:

```jsx
```erb
<%= sort_link(@q, :name, hide_indicator: true) %>
```

Expand All @@ -219,15 +220,15 @@ Ransack's `sort_url` helper is like a `sort_link` but returns only the url

`sort_url` has the same API as `sort_link`:

```jsx
```erb
<%= sort_url(@q, :name, default_order: :desc) %>
```

```jsx
```erb
<%= sort_url(@q, :last_name, [:last_name, 'first_name asc']) %>
```

```jsx
```erb
<%= sort_url(@q, :last_name, %i(last_name first_name),
default_order: { last_name: 'asc', first_name: 'desc' }) %>
```
Expand All @@ -238,15 +239,15 @@ The `NULLS FIRST` and `NULLS LAST` options can be used to determine whether null

You may want to configure it like this:

```jsx
```ruby
Ransack.configure do |c|
c.postgres_fields_sort_option = :nulls_first # or :nulls_last
end
```

To treat nulls as having the lowest or highest value respectively. To force nulls to always be first or last, use

```jsx
```ruby
Ransack.configure do |c|
c.postgres_fields_sort_option = :nulls_always_first # or :nulls_always_last
end
Expand All @@ -258,7 +259,7 @@ See this feature: https://www.postgresql.org/docs/13/queries-order.html

In order to request PostgreSQL to do a case insensitive sort for all string columns of a model at once, Ransack can be extended by using this approach:

```jsx
```ruby
module RansackObject

def self.included(base)
Expand All @@ -273,7 +274,7 @@ module RansackObject
end
```

```jsx
```ruby
class UserWithManyAttributes < ActiveRecord::Base
include RansackObject
end
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/getting-started/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: Sorting

You can add a form to capture sorting and filtering options together.

```jsx
```erb
<div class="filters" id="filtersSidebar">
<header class="filters-header">
<div class="filters-header-content">
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/going-further/acts-as-taggable-on.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ chances are you might want to search on tagged fields. Follow the instructions t

You can call the tagging field anything you like, it just needs to be plural. No migration is needed as this is stored in the internal ActsAsTaggable tables (`tags` and `taggings`).

```rb
```ruby
class Task < ApplicationRecord
acts_as_taggable_on :projects
end
Expand All @@ -26,7 +26,7 @@ Add a field to strong params in the controller. Use the singular name with `_lis

`app/controllers/tasks_controller.rb`

```rb
```ruby
def strong_params
params
.require(:tasks)
Expand All @@ -37,7 +37,7 @@ def strong_params

We need to `send` the tag fieldname to our model, also using the singular naming.

```
```erb
<div class='form-group'>
<%= f.label :project_list %>
<%= f.text_field :project_list, value: @task.send(:project_list).to_s %>
Expand All @@ -50,7 +50,7 @@ Now we can collect our data via the form, with tags separated by commas.

Imagine you have the following two instances of `Task`:

```rb
```ruby
{ id: 1, name: 'Clean up my room', projects: [ 'Home', 'Personal' ] }
{ id: 2, name: 'Complete math exercises', projects: [ 'Homework', 'Study' ] }
```
Expand Down Expand Up @@ -97,7 +97,7 @@ In Option D we allow the user to select a list of valid tags and then search aga

ActsAsTaggableOn allows scoping of tags based on another field on the model. Suppose we have a `language` field on the model, as an effective second level key. We would adjust our model to look like this:

```rb
```ruby
class Task < ApplicationRecord
acts_as_taggable_on :projects
acts_as_taggable_tenant :language
Expand All @@ -106,7 +106,7 @@ end

The Ransack search is then filtered using the `for_tenant` method

```
```erb
<div class='form-group'>
<%= f.label :projects_name, 'Project' %>
<%= f.select :projects_name_in, ActsAsTaggableOn::Tag.for_tenant('fr').distinct.order(:name).pluck(:name) %>
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/going-further/exporting-to-csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Exporting to CSV

Example downloading a csv file preserving ransack search, based on [this gist](https://gist.github.com/pama/adff25ed1f4b796ce088ea362a08e1c5)

```jsx title='index.html.erb'
```ruby title='index.html.erb'
<h1>Users</h1>

<%= search_form_for @q, url: dashboard_index_path do |f| %>
Expand All @@ -30,7 +30,7 @@ Example downloading a csv file preserving ransack search, based on [this gist](h
<% end %>
```

```jsx title='user.rb'
```ruby title='user.rb'
require 'csv'

class User < ApplicationRecord
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Ransack is supported for Rails 7.0, 6.x on Ruby 2.6.6 and later.

To install `ransack` and add it to your Gemfile, run

```jsx title='Gemfile'
```ruby title='Gemfile'
gem 'ransack'
```

### Bleeding edge

If you would like to use the latest updates not yet published to RubyGems, use the `main` branch:

```jsx title='Gemfile'
```ruby title='Gemfile'
gem 'ransack', :github => 'activerecord-hackery/ransack', :branch => 'main'
```

Expand Down
1 change: 1 addition & 0 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const config = {
prism: {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
additionalLanguages: ['ruby', 'erb'],
},
}),
};
Expand Down