Git Product home page Git Product logo

rubanok's Introduction

Gem Version Build

Rubanok

Rubanok provides a DSL to build parameters-based data transformers.

📖 Read the introduction post: "Carve your controllers like Papa Carlo"

The typical usage is to describe all the possible collection manipulation for REST index action, e.g. filtering, sorting, searching, pagination, etc..

So, instead of:

class CourseSessionController < ApplicationController
  def index
    @sessions = CourseSession
      .search(params[:q])
      .by_course_type(params[:course_type_id])
      .by_role(params[:role_id])
      .paginate(page_params)
      .order(ordering_params)
  end
end

You have:

class CourseSessionController < ApplicationController
  def index
    @sessions = rubanok_process(
      # pass input
      CourseSession.all,
      # pass params
      params,
      # provide a processor to use
      with: CourseSessionsProcessor
    )
  end
end

Or we can try to infer all the configuration for you:

class CourseSessionController < ApplicationController
  def index
    @sessions = rubanok_process(CourseSession.all)
  end
end

Requirements:

  • Ruby ~> 2.7
  • (optional*) Rails >= 6.0 (see older releases for Rails <6 support)

* This gem has no dependency on Rails.

Sponsored by Evil Martians

Installation

Add to your Gemfile:

gem "rubanok"

And run bundle install.

Usage

The core concept of this library is a processor (previously called plane or hand plane, or "рубанок" in Russian). Processor is responsible for mapping parameters to transformations.

From the example above:

class CourseSessionsProcessor < Rubanok::Processor
  # You can map keys
  map :q do |q:|
    # `raw` is an accessor for input data
    raw.search(q)
  end
end

# The following code
CourseSessionsProcessor.call(CourseSession.all, q: "xyz")

# is equal to
CourseSession.all.search("xyz")

You can map multiple keys at once:

class CourseSessionsProcessor < Rubanok::Processor
  DEFAULT_PAGE_SIZE = 25

  map :page, :per_page do |page:, per_page: DEFAULT_PAGE_SIZE|
    raw.paginate(page: page, per_page: per_page)
  end
end

There is also match method to handle values:

class CourseSessionsProcessor < Rubanok::Processor
  SORT_ORDERS = %w[asc desc].freeze
  SORTABLE_FIELDS = %w[id name created_at].freeze

  match :sort_by, :sort do
    having "course_id", "desc" do
      raw.joins(:courses).order("courses.id desc nulls last")
    end

    having "course_id", "asc" do
      raw.joins(:courses).order("courses.id asc nulls first")
    end

    # Match any value for the second arg
    having "type" do |sort: "asc"|
      # Prevent SQL injections
      raise "Possible injection: #{sort}" unless SORT_ORDERS.include?(sort)
      raw.joins(:course_type).order("course_types.name #{sort}")
    end

    # Match any value
    default do |sort_by:, sort: "asc"|
      raise "Possible injection: #{sort}" unless SORT_ORDERS.include?(sort)
      raise "The field is not sortable: #{sort_by}" unless SORTABLE_FIELDS.include?(sort_by)
      raw.order(sort_by => sort)
    end
  end

  # strict matching; if Processor will not match parameter, it will raise Rubanok::UnexpectedInputError
  # You can handle it in controller, for example, with sending 422 Unprocessable Entity to client
  match :filter, fail_when_no_matches: true do
    having "active" do
      raw.active
    end

    having "finished" do
      raw.finished
    end
  end
end

By default, Rubanok will not fail if no matches found in match rule. You can change it by setting: Rubanok.fail_when_no_matches = true. If in example above you will call CourseSessionsProcessor.call(CourseSession, filter: 'acitve'), you will get Rubanok::UnexpectedInputError: Unexpected input: {:filter=>'acitve'}.

NOTE: Rubanok only matches exact values; more complex matching could be added in the future.

Nested processors

You can use the .process method to define sub-processors (or nested processors). It's useful when you use nested params, for example:

class CourseSessionsProcessor < Rubanok::Processor
  process :filter do
    match :status do
      having "draft" do
        raw.where(draft: true)
      end

      having "deleted" do
        raw.where.not(deleted_at: nil)
      end
    end

    # You can also use .map or even .process here
  end
end

Default transformation

Sometimes it's useful to perform some transformations before any rule is activated.

There is a special prepare method which allows you to define the default transformation:

class CourseSearchQueryProcessor < Rubanok::Processor
  prepare do
    next if raw&.dig(:query, :bool)

    {query: {bool: {filters: []}}}
  end

  map :ids do |ids:|
    raw.dig(:query, :bool, :filters) << {terms: {id: ids}}
    raw
  end
end

The block should return a new initial value for the raw input or nil (no transformation required).

The prepare callback is not executed if no params match, e.g.:

CourseSearchQueryProcessor.call(nil, {}) #=> nil

# But
CourseSearchQueryProcessor.call(nil, {ids: [1]}) #=> {query {bool: {filters: [{terms: {ids: [1]}}]}}}

# Note that we can omit the first argument altogether
CourseSearchQueryProcessor.call({ids: [1]})

Getting the matching params

Sometimes it could be useful to get the params that were used to process the data by Rubanok processor (e.g., you can use this data in views to display the actual filters state).

In Rails, you can use the #rubanok_scope method for that:

class CourseSessionController < ApplicationController
  def index
    @sessions = rubanok_process(CourseSession.all)
    # Returns the Hash of params recognized by the CourseSessionProcessor.
    # For example:
    #
    #    params == {q: "search", role_id: 2, date: "2019-08-22"}
    #    @session_filter == {q: "search", role_id: 2}
    @sessions_filter = rubanok_scope(
      params.permit(:q, :role_id),
      with: CourseSessionProcessor
    )

    # You can omit all the arguments
    @sessions_filter = rubanok_scope #=> equals to rubanok_scope(params, with: implicit_rubanok_class)
  end
end

You can also accesss rubanok_scope in views (it's a helper method).

Rule activation

Rubanok activates a rule by checking whether the corresponding keys are present in the params object. All the fields must be present to apply the rule.

Some fields may be optional, or perhaps even all of them. You can use activate_on and activate_always options to mark something as an optional key instead of a required one:

# Always apply the rule; use default values for keyword args
map :page, :per_page, activate_always: true do |page: 1, per_page: 2|
  raw.page(page).per(per_page)
end

# Only require `sort_by` to be preset to activate sorting rule
match :sort_by, :sort, activate_on: :sort_by do
 # ...
end

By default, Rubanok ignores empty param values (using #empty? under the hood) and will not run matching rules on those values. For example: { q: "" } and { q: nil } won't activate the map :q rule.

You can change this behaviour by specifying ignore_empty_values: true option for a particular rule or enabling this behaviour globally via Rubanok.ignore_empty_values = true (enabled by default).

Input values filtering

For complex input types, such as arrays, it might be useful to prepare the value before passing to a transforming block or prevent the activation altogether.

We provide a filter_with: option for the .map method, which could be used as follows:

class PostsProcessor < Rubanok::Processor
  # We can pass a Proc
  map :ids, filter_with: ->(vals) { vals.reject(&:blank?).presence } do |ids:|
    raw.where(id: ids)
  end

  # or define a class method
  def self.non_empty_array(val)
    non_blank = val.reject(&:blank?)
    return if non_blank.empty?

    non_blank
  end

  # and pass its name as a filter_with value
  map :ids, filter_with: :non_empty_array do |ids:|
    raw.where(id: ids)
  end
end

# Filtered values are used in rules
PostsProcessor.call(Post.all, {ids: ["1", ""]}) == Post.where(id: ["1"])

# When filter returns empty value, the rule is not applied
PostsProcessor.call(Post.all, {ids: [nil, ""]}) == Post.all

Testing

One of the benefits of having modification logic contained in its own class is the ability to test modifications in isolation:

# For example, with RSpec
RSpec.describe CourseSessionsProcessor do
  let(:input) { CourseSession.all }
  let(:params) { {} }

  subject { described_class.call(input, params) }

  specify "searching" do
    params[:q] = "wood"

    expect(subject).to eq input.search("wood")
  end
end

Now in your controller you only have to test that the specific plane is applied:

RSpec.describe CourseSessionController do
  subject { get :index }

  specify do
    expect { subject }.to have_rubanok_processed(CourseSession.all)
      .with(CourseSessionsProcessor)
  end
end

NOTE: input matching only checks for the class equality.

To use have_rubanok_processed matcher you must add the following line to your spec_helper.rb / rails_helper.rb (it's added automatically if RSpec defined and RAILS_ENV/RACK_ENV is equal to "test"):

require "rubanok/rspec"

Rails vs. non-Rails

Rubanok does not require Rails, but it has some useful Rails extensions such as rubanok_process helper for controllers (included automatically into ActionController::Base and ActionController::API).

If you use ActionController::Metal you must include the Rubanok::Controller module yourself.

Processor class inference in Rails controllers

By default, rubanok_process uses the following algorithm to define a processor class: "#{controller_path.classify.pluralize}Processor".safe_constantize.

You can change this by overriding the #implicit_rubanok_class method:

class ApplicationController < ActionController::Smth
  # override the `implicit_rubanok_class` method
  def implicit_rubanok_class
    "#{controller_path.classify.pluralize}Scoper".safe_constantize
  end
end

Now you can use it like this:

class CourseSessionsController < ApplicationController
  def index
    @sessions = rubanok_process(CourseSession.all, params)
    # which equals to
    @sessions = CourseSessionsScoper.call(CourseSession.all, params.to_unsafe_h)
  end
end

NOTE: the planish method is still available and it uses #{controller_path.classify.pluralize}Plane".safe_constantize under the hood (via the #implicit_plane_class method).

Using with RBS/Steep

Read "Climbing Steep hills, or adopting Ruby 3 types with RBS" for the context.

Rubanok comes with Ruby type signatures (RBS).

To use them with Steep, add library "rubanok" to your Steepfile.

Since Rubanok provides DSL with implicit context switching (via instance_eval), you need to provide type hints for the type checker to help it figure out the current context. Here is an example:

class MyProcessor < Rubanok::Processor
  map :q do |q:|
    # @type self : Rubanok::Processor
    raw
  end

  match :sort_by, :sort, activate_on: :sort_by do
    # @type self : Rubanok::DSL::Matching::Rule
    having "status", "asc" do
      # @type self : Rubanok::Processor
      raw
    end

    # @type self : Rubanok::DSL::Matching::Rule
    default do |sort_by:, sort: "asc"|
      # @type self : Rubanok::Processor
      raw
    end
  end
end

Yeah, a lot of annotations 😞 Welcome to the type-safe world!

Questions & Answers

  • Where to put my processor/plane classes?

I put mine under app/planes (as <resources>_plane.rb) in my Rails app.

  • I don't like the naming ("planes" ✈️?), can I still use the library?

Good news—the default naming has been changed. "Planes" are still available if you prefer them (just like me 😉).

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/rubanok.

License

The gem is available as open source under the terms of the MIT License.

rubanok's People

Contributors

baweaver avatar earendil95 avatar mortonfox avatar olleolleolle avatar palkan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rubanok's Issues

Support nested params

Is your feature request related to a problem? Please describe.

We have an API design where params are nested, like filter[state]=paid.

Describe the solution you'd like

It would be great if map would accept hash/array as a path in params.

Describe alternatives you've considered

  • Changing API to flat structure
  • Writing preprocessor

Deprecate activate_always/activate_on

Is your feature request related to a problem? Please describe.

Adding activate_always / activate_on is boring, we can do better.

Describe the solution you'd like

We can rely on block signature to infer activation rules:

  • if all kwrags have default values -> activate always
  • if some kwargs have default values -> active on these keys

Deprecate fail_when_no_matches

Is your feature request related to a problem? Please describe.

It's ugly.

Describe the solution you'd like

It could be done with the default rule.

Get values of filters in `match` block

Is it possible to get the actual value of matching objects in the match block?
For example, let's say we have this code:

class SomePlane < Rubanok::Plane
  process :filter do
    match :some_value do
      having true do
        # ...
      end
    end
  end
end

In this case some_value should include true to match the condition. But what if it contains some other value, and we want to use some_value.is_a?(TrueClass) or any other custom condition for some_value to proceed with the having block?
For example:

class SomePlane < Rubanok::Plane
  process :filter do
    match :some_value do |some_value:|
      having some_value.is_a?(TrueClass) || some_value.blank? do
        # ...
      end
    end
  end
end

Maybe it's already possible to use it like this, but I am not sure, it's not included in README, and I tried to write a code like this and it doesn't work.

naming?

I really like your approach here!

I do not like the "plane" and "planish" terminology. It doesn't mean anything, nor is it obviously connected to the name "rubanok" (unless you know Russian maybe!)

I think that if I write code using rubanok, future developers when they see a "SomethingPlane" class are going to be confused, and especially confused when they see a planish method in a controller. It's unclear what it does, and it's unclear it's related to Rubanok.

I would suggest either "query" or "transform(er)" instead as terminology. For the demonstrated use cases, a "query object" seems to describe really well what rubanok objects do. But rubanok may not be intended to be limited to or focused on "query objects", but to general transformations?

I also really like putting the name of the gem in any automatically-mixed-into-Rails methods that a gem adds. With some rails devs adding lots of gems, it can be very confusing to figure out where a method comes from, and the gem name on the auto-mixed-in-method makes it a lot clearer.

So maybe:

class CourseSessionsQuery < Rubanok::Query
  # ...
end

class CourseSessionController < ApplicationController
  def index
    @sessions = rubanok_query(CourseSession.all)
  end
end

Or

class CourseSessionsTransformer < Rubanok::Transformer
  # ...
end

class CourseSessionController < ApplicationController
  def index
    @sessions = rubanok_transform(CourseSession.all)
  end
end

(Or "Transformer" on the class name, but query on the controller-mixin-in, since in a controller that's pretty much what it's for?)

Something like this would make me a lot less reluctant to try out rubanok in a project. The existing naming just leaves me feel like I'm going to confuse my present and future colleagues.

I know I can easily insert my own code into Rubanok to use whatever I like... but when you can't easily google for "what's going on here" based on what you find in the code -- or if the docs and examples you find don't match what you found in the code, it's another thing that confuses developers. It's important to me that code that comes from gems be very clear about where it comes from and what it does, based on the naming and ability to google for that naming.

current_scopes idea

Just want to share something I've been useful on my projects that could be added to this gem in the future. Let me know what you think @palkan.

class ApplicationController < ActionController::Base 
  def current_scopes 
    @current_scopes ||= implicit_plane_class.rules.map(&:fields).flatten.yield_self { |keys| request.parameters.slice(*keys) } 
  end 
end

undefined method `helper_method' for ActionController::API

I got this message after upgrading to the latest version (it's an easy fix though):

/home/vmbox/.asdf/installs/ruby/2.6.1/lib/ruby/gems/2.6.0/gems/rubanok-0.2.0/lib/rubanok/rails/controller.rb:12:in `block in <module:Controller>': undefined method `helper_method' for ActionController::API:Class (NoMethodError)

Just need to check if class respond_to?(:helper_method) before adding those helpers.

seamless pagy integration

Hey @palkan, how you're doing pagination with rubanok? I've been using rubanok + pagy, and writing code like this all over the place:

scope = rubanok_process lesson.comments.arranged, with: CommentsProcessor
@pagy, @comments = pagy(scope, items: 10)

Do you think there is any way to refactor it or expose @pagy to controllers through the Processor class?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.