Sha256: 2346a6105fb943b609bc2f7ba60cee61663efef0f82a7ad0706f1ee704bcc8f6

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module Adminos::FlagAttrs
  extend ActiveSupport::Concern

  module ClassMethods
    def flag_attrs(*args)
      options = args.extract_options!

      on = args.delete(:on) || true
      off = args.delete(:off) || false

      args.each do |name|
        define_method :"set_#{name}_on" do
          update_attribute(name, on)
        end

        define_method :"set_#{name}_off" do
          update_attribute(name, off)
        end

        (class << self; self; end).class_eval do
          define_method :"set_#{name}_on" do
            update_all(name => on)
          end

          define_method :"set_each_#{name}_on" do
            all.each { |object| object.update_attribute(name, on) }
          end

          define_method :"set_#{name}_off" do
            update_all(name => off)
          end

          define_method :"set_each_#{name}_off" do
            all.each { |object| object.update_attribute(name, off) }
          end
        end

        scope :"#{name}", -> { where(name => true) }
        scope :"not_#{name}", -> { where(name => false) }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
adminos-1.0.0.pre.rc.5 lib/adminos/helpers/models/flag_attrs.rb
adminos-1.0.0.pre.rc.4 lib/adminos/helpers/models/flag_attrs.rb
adminos-1.0.0.pre.rc.3 lib/adminos/helpers/models/flag_attrs.rb
adminos-1.0.0.pre.rc.2 lib/adminos/helpers/models/flag_attrs.rb
adminos-1.0.0.pre.rc.1 lib/adminos/helpers/models/flag_attrs.rb