Module: Content::ItemClassMethods

Included in:
Item
Defined in:
lib/content/item_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



10
11
12
# File 'lib/content/item_class_methods.rb', line 10

def connection()
  $adapter ||= establish_connection
end

#create(attributes = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/content/item_class_methods.rb', line 37

def create(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    object = new(attributes)
    yield(object) if block_given?
    object.save
    object
  end
end

#delete(id) ⇒ Object



67
68
69
# File 'lib/content/item_class_methods.rb', line 67

def delete(id)
  destroy id
end

#destroy(id) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/content/item_class_methods.rb', line 59

def destroy(id)
  if id.is_a?(Array)
    id.map { |one_id| destroy(one_id) }
  else
    find(id).destroy
  end
end

#establish_connection(options = {}) ⇒ Object



3
4
5
6
7
8
# File 'lib/content/item_class_methods.rb', line 3

def establish_connection(options = {})
  conn_options = ::YAML::load_file('config/content.yml')[::RAILS_ENV]
  conn_options.merge!(options) unless options.nil?
  conn_type = conn_options[:type] || conn_options["type"] || :cabinet
  $adapter = "content/adapters/#{conn_type}_adapter".camelize.constantize.new(conn_options)
end

#human_attribute_name(attr_name) ⇒ Object



33
34
35
# File 'lib/content/item_class_methods.rb', line 33

def human_attribute_name(attr_name)
  attr_name.to_s.humanize
end

#human_name(options = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/content/item_class_methods.rb', line 25

def human_name(options = {})
  defaults = self_and_descendants_from_active_record.map do |klass|
    :"#{klass.name.underscore}"
  end 
  defaults << name
  defaults.shift.to_s.humanize
end

#self_and_descendants_from_active_recordObject

nodoc:



14
15
16
17
18
19
20
21
22
23
# File 'lib/content/item_class_methods.rb', line 14

def self_and_descendants_from_active_record#nodoc:
  klass = self
  classes = [klass]
  while klass != klass.base_class  
    classes << klass = klass.superclass
  end
  classes
rescue
  [self]
end

#update(id, attributes) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/content/item_class_methods.rb', line 48

def update(id, attributes)
  if id.is_a?(Array)
    idx = -1
    id.collect { |one_id| idx += 1; update(one_id, attributes[idx]) }
  else
    object = find(id)
    object.update_attributes(attributes)
    object
  end
end