Module: LoyalCore::ActsAsPublishStatusAble::ClassMethods

Defined in:
lib/loyal_core/acts/publish_status_able.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#loyal_core_acts_as_publish_status_able(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/loyal_core/acts/publish_status_able.rb', line 12

def loyal_core_acts_as_publish_status_able *args
  _config = ::LoyalCore::ConfigUtil.new(
    {:key => 'init',        :value => 0,      :desc => '初始'},
    {:key => 'published',   :value => 2**1,   :desc => '已发布'},
    {:key => 'blocked',    :value => 2**2,   :desc => '已屏蔽'}
  )

  define_singleton_method :publish_status_config do
    _config
  end

  _config.pure_keys.each do |key, config|
    define_method :"publish_status_#{key}?" do
      self.publish_status_key == key
    end
  end

  scope :of_publish_status, ->(status_name) do
    if status_name
      status_name = status_name.to_s unless status_name.is_a?(String)
      config = _config.at(status_name)
      status = config.nil? ? nil : config.value

      where(:publish_status => status)
    end
  end

  scope :published, -> do
    of_publish_status('published')
  end

  include InstanceMethods
end