Sha256: 232f93ac83243154fc6fb1e4c05f48b8e82405f33b1da2d1b3fef9e019a5b21f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module SudoAttributes

  module Base
    def sudo_attr_protected(*attrs)
      Private::set_attributes(self, attrs, :protected)
    end

    def sudo_attr_accessible(*attrs)
      Private::set_attributes(self, attrs, :accessible)
    end
  end

  module Private

    def self.set_attributes(klass, attrs, type)
      unless attrs.empty?
        raise "Invalid argument passed to has_sudo_attributes" unless attrs.all? {|a| a.is_a? Symbol }

        klass.send("attr_#{type}", *attrs)
      end

      klass.extend SudoAttributes::ClassMethods
      klass.send :include, SudoAttributes::InstanceMethods
    end
  end
  
  # Added to ActiveRecord model only if has_sudo_attributes is called
  module ClassMethods
    def sudo_create(attributes=nil)
      instance = sudo_new(attributes)
      instance.save
      instance
    end

    def sudo_create!(attributes=nil)
      instance = sudo_new(attributes)
      instance.save!
      instance
    end

    def sudo_new(attributes=nil)
      instance = new(nil)
      instance.send(:attributes=, attributes, false)
      instance
    end
  end
 
  module InstanceMethods
    def sudo_update_attributes(new_attributes)
      self.send(:attributes=, new_attributes, false)
      save
    end

    def sudo_update_attributes!(new_attributes)
      self.send(:attributes=, new_attributes, false)
      save!
    end
  end
end

ActiveRecord::Base.extend SudoAttributes::Base

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sudo_attributes-0.5.0 lib/sudo_attributes.rb