Created
October 1, 2020 01:14
-
-
Save ayamomiji/b42aa9310189df66f5b65fad785cf6b0 to your computer and use it in GitHub Desktop.
include module with parameter, a prettier version imo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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