Refine::Rails
Short description and motivation.
Usage
How to use my plugin.
Installation (if using BT see BulletTrain installation below)
Add this line to your application's Gemfile:
gem "refine-rails"
And then execute:
$ bundle
Or install it yourself as:
$ gem install refine-rails
Installing the JavaScript package:
$ yarn add @hammerstone/refine-stimulus
Also, make sure that your project uses jquery
and binds it as window.$
. Required for catching events dispatched by select2
dropdowns.
Also note that there's currently a bug with esbuild and stimulus 3.0 compatibility.
Importing and Registering the Stimulus Controllers
Where you normally import your Stimulus controllers, add the following lines:
import { controllerDefinitions as refineControllers } from "@hammerstone/refine-stimulus"
Stimulus.load(refineControllers)
If loading in Bullet Train,the load command is
application.load(refineControllers)
To manually register (or extend or provide your own replacement for) each Stimulus controller:
import {
AddController,
DefaultsController,
DeleteController,
FormController,
StateController,
StoredFilterController,
UpdateController
} from "@hammerstone/refine-stimulus"
Stimulus.register('refine--add', AddController)
Stimulus.register('refine--defaults', DefaultsController)
Stimulus.register('refine--delete', DeleteController)
Stimulus.register('refine--form', FormController)
Stimulus.register('refine--state', StateController)
Stimulus.register('refine--stored-filter', StoredFilterController)
Stimulus.register('refine--update', UpdateController)
Contributing
Contribution directions go here.
Local JavaScript Development
From this repo's directory:
$ yarn
$ yarn link
$ yarn build --watch
From the directory of the project including this package:
yarn link @hammerstone/refine-stimulus
Running yarn
again from your project's directory will revert back to the published version of the package on npm.
Note: Because of a weird behavior in how yarn link
works, you might have to make sure the project including this package includes a few of this package's dependencies (e.g. lodash, stimulus, etc)
Release
- Publish the gem with a new version number
- Copy the version number in package.json
- run
yarn build
. This will prepare the different javascript outputs - run
yarn pack
. This will create a new .tgz
file for the new version - run
yarn publish <tgz filename> --new-version <version number in package.json>
- remove the
*.tgz
file
Bullet Train Installation
Add ruby gem
source "https://yourAPIKey@gem.fury.io/hammerstonedev" do
gem "refine-rails"
end
Installing the JavaScript package:
$ yarn add @hammerstone/refine-stimulus
In app/javascript/controllers/index.js
add
import { controllerDefinitions as refineControllers } from "@hammerstone/refine-stimulus"
application.load(refineControllers)
TO FIX
- Add Hammerstone routes to the routes file (these should be in the gem)
# Hammerstone routes
namespace :hammerstone do
resource :refine_blueprint, only: [:show, :update, :create]
put "update_stable_id", to: "refine_blueprints#update_stable_id"
namespace :refine do
resources :stored_filters, only: [:create, :index, :show, :new, :update, :edit] do
get "editor", on: :collection
end
end
end
TO FIX
class Account::ApplicationController < ApplicationController
include Account::Controllers::Base
def ensure_onboarding_is_complete
return false unless super
unless adding_team? || accepting_invitation?
end
true
end
def child_filter_class
self.class.name.gsub(/^Account::/, "").gsub(/Controller$/, "Filter").constantize
rescue NameError => _
nil
end
def apply_filter(current_scope = nil)
if child_filter_class.present?
@stored_filter = nil
@stable_id = stable_id
@refine_filter = if stable_id
Hammerstone::Refine::Stabilizers::UrlEncodedStabilizer.new.from_stable_id(id: stable_id, initial_query: @parent_object.send(@child_collection))
elsif @stored_filter
@stored_filter.refine_filter
else
child_filter_class.new([], @parent_object.send(@child_collection))
end
instance_variable_set(children_instance_variable_name, @refine_filter.get_query)
instance_variable_set(children_instance_variable_name, children_instance_variable.merge(current_scope)) if current_scope
end
end
def filter_params
params.permit(:filter, :stable_id, :blueprint, :conditions, :clauses, :name, :tab, :site_id, :workspace_id, :product_id, :q, :stored_filter_id)
end
def stored_filter_id
filter_params[:stored_filter_id]
end
def stable_id
filter_params[:stable_id]
end
def children_instance_variable_name
"@" + self.class.name.gsub(/^Account::/, "").gsub(/Controller$/, "").split("::").last.underscore
end
def children_instance_variable
instance_variable_get(children_instance_variable_name)
end
end
TODO move locales files to gem?
Do we want to include the filter_link_controller, filter_builder_partial when releasing to non BT customers?