Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
Created October 1, 2020 01:14
Show Gist options
  • Save ayamomiji/b42aa9310189df66f5b65fad785cf6b0 to your computer and use it in GitHub Desktop.
Save ayamomiji/b42aa9310189df66f5b65fad785cf6b0 to your computer and use it in GitHub Desktop.
include module with parameter, a prettier version imo
# adds `acts_as_list` and a `ordered` scope with given scope
# usage:
# include Listable
# include Listable[scope: :user]
module Listable
extend ActiveSupport::Concern
included do
include ListableModule.new
end
class << self
def [](acts_as_list_params)
ListableModule.new(acts_as_list_params)
end
end
class ListableModule < Module
def initialize(acts_as_list_params = nil)
@acts_as_list_params = acts_as_list_params
end
def included(base)
acts_as_list_params = @acts_as_list_params
base.class_eval do
scope :ordered, -> { order(position: :asc) }
acts_as_list acts_as_list_params
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment