Module: Accessitude::ClassMethods

Defined in:
lib/accessitude.rb

Instance Method Summary collapse

Instance Method Details

#accessitudeObject



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
45
46
47
48
49
50
51
52
53
# File 'lib/accessitude.rb', line 20

def accessitude
  
  # Array of all accessible attributes
  # @return [Array]
  define_method :accessitude_attrs do
    self.class.accessitude_attrs
  end

  # Check if any of the accessible attributes are not _blank_
  # @return [Boolean]
  define_method :has_accessitude_data do
    self.attributes.values_at( *self.accessitude_attrs ).compact.delete_if { |attr| attr.blank? }.size > 0
  end
  
  # Set attributes of instance only using attr_accessible params
  define_method :attributes_from_params do |params|
    self.attributes = params.slice(*accessitude_attrs)
  end
  
  class << self
    
    # Array of all accessible attributes
    # @return [Array]
    def accessitude_attrs
      self.attr_accessible[:default].to_a - ['','default']
    end
    
    # Create a new instance only using attr_accessible params
    def new_from_params(params)
      self.create( params.slice(*accessitude_attrs) )
    end        
    
  end          
end