Module: SimplyCouch::ClassMethods::Base

Included in:
Model::ClassMethods
Defined in:
lib/simply_couch/class_methods_base.rb

Instance Method Summary collapse

Instance Method Details

#_find_property(name) ⇒ Object



33
34
35
# File 'lib/simply_couch/class_methods_base.rb', line 33

def _find_property(name)
  properties.find{|property| property.name == name}
end

#after_commitObject

Fix for paperclip > 3.5.2



54
# File 'lib/simply_couch/class_methods_base.rb', line 54

def after_commit(*); end

#attr_accessible(*args) ⇒ Object



28
29
30
31
# File 'lib/simply_couch/class_methods_base.rb', line 28

def attr_accessible(*args)
  @_accessible_attributes ||= []
  @_accessible_attributes += args.to_a
end

#attr_protected(*args) ⇒ Object



23
24
25
26
# File 'lib/simply_couch/class_methods_base.rb', line 23

def attr_protected(*args)
  @_protected_attributes ||= []
  @_protected_attributes += args.to_a
end

#bulk_update(ids, pairs) ⇒ Object

Get documents by ids and bulk update attributes



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simply_couch/class_methods_base.rb', line 57

def bulk_update(ids, pairs)
  # Bulk load documents
  # Map to doc
  # Filter out errors, or none founds (compact)
  # Select the Couch objects
  docs = database.couchrest_database.bulk_load(ids)['rows'].map{|r| r['doc']}.compact.select{|d| d.is_a?(SimplyCouch::Model)}
  for doc in docs
    pairs.each_pair do |k, v|
      doc.send("#{k}=", v) if doc.respond_to?("#{k}=")
    end
    doc.save # Should become a bulk update in the future
  end
end

#find_association_class_name(association_name) ⇒ Object

Namespace aware method of creating proper class names



38
39
40
# File 'lib/simply_couch/class_methods_base.rb', line 38

def find_association_class_name(association_name)
  (name.split('::')[0..-2] + [association_name.to_s.singularize.camelize]).join('::')
end

#foreign_keyObject



15
16
17
# File 'lib/simply_couch/class_methods_base.rb', line 15

def foreign_key
  name.underscore.gsub('/','__').gsub('::','__') + "_id"
end

#foreign_propertyObject



19
20
21
# File 'lib/simply_couch/class_methods_base.rb', line 19

def foreign_property
  name.underscore.gsub('/','__').gsub('::','__')
end

#get_class_from_name(klass_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/simply_couch/class_methods_base.rb', line 4

def get_class_from_name(klass_name)
  # Try to find class from property definition
  if property = _find_property(klass_name) and property.respond_to?(:options) and property.options[:class_name].present?
    property.options[:class_name].constantize
  else # Fall back to name guessing
    base = klass_name.to_s.gsub('__','/')
    base = base.classify unless base[0,1] =~ /[A-Z]/
    base.constantize
  end
end

#primary_keyObject

More compatibility with active record plugins



43
44
45
# File 'lib/simply_couch/class_methods_base.rb', line 43

def primary_key
  'id'
end

#view(view_name, options) ⇒ Object



47
48
49
50
51
# File 'lib/simply_couch/class_methods_base.rb', line 47

def view(view_name, options)
  options[:reduce] = options.delete(:reduce_function) if options[:reduce_function]
  options[:map] = options.delete(:map_function) if options[:map_function]
  super(view_name, options)
end