Ruby on rails RubyonRails:从关注点调用类方法

Ruby on rails RubyonRails:从关注点调用类方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,在Rails中,可以从包含关注点的类调用方法吗?即: class Foo < ApplicationRecord include Encryptable def self.encrypted_attributes %i[attr_1 attr_2] end end module Encryptable extend ActiveSupport::Concern included do self.encrypted_attributes do |a

在Rails中,可以从包含关注点的类调用方法吗?即:

class Foo < ApplicationRecord
  include Encryptable

  def self.encrypted_attributes
     %i[attr_1 attr_2]
  end
end

module Encryptable
  extend ActiveSupport::Concern

  included do
    self.encrypted_attributes do |attr|
      define_method("#{attr}=") do |arg|
        # do some stuff
      end

      define_method("#{attr}") do
        # do some stuff
      end
    end
  end
end

Ruby是一种脚本语言,顺序很重要。以下是可以做到的:

class Foo < ApplicationRecord
  def self.encrypted_attributes
     %i[attr_1 attr_2]
  end

  # OK, now we have self.encrypted_attributes defined
  include Encryptable
end
class Foo

更多信息:。

的确如此。非常感谢。
class Foo < ApplicationRecord
  def self.encrypted_attributes
     %i[attr_1 attr_2]
  end

  # OK, now we have self.encrypted_attributes defined
  include Encryptable
end