Question

In reality, I want to configure my gem.

require "foo/version"
require "foo/bar"
module Foo
end

require 'securerandom'
require 'active_support'
module Foo
  module Bar
    extend ActiveSupport::Concern

    included do
      include ActiveModel::SecurePassword

      # I would to change this line to something like
      # has_secure_password validations: (validate? true : false)
      has_secure_password validations: true
    end

    module ClassMethods
    end

    extend ClassMethods
  end
end

I want to load my gem from from the model, something like:

class Admin < ActiveRecord::Base
  foo_bar validate: false
end

But I have no clue what the Ruby idiom is for something like this. How can achieve this?

Was it helpful?

Solution

You need to define class method foo_bar within your module which will accept hash and behave accordingly. You could for example do:

module Foo
  module Bar
    extend ActiveSupport::Concern

    included do
      include ActiveModel::SecurePassword
    end

    module ClassMethods
      def foo_bar(options)
        has_secure_password validations: options[:validate]
      end
    end

    extend ClassMethods
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top