Module: CouchRest::Model::Designs::ClassMethods

Defined in:
lib/couchrest/model/designs.rb

Instance Method Summary collapse

Instance Method Details

#default_per_pageObject

The models number of documents to return by default when performing pagination. Returns 25 unless explicitly overridden via paginates_per



82
83
84
# File 'lib/couchrest/model/designs.rb', line 82

def default_per_page
  @_default_per_page || 25
end

#design(*args, &block) ⇒ Object

Define a Design Document associated with the current model.

This class method supports several cool features that make it much easier to define design documents.

Adding a prefix allows you to associate multiple design documents with the same model. This is useful if you'd like to split your designs into seperate use cases; one for regular search functions and a second for stats for example.

# Create a design doc with id _design/Cats
design do
  view :by_name
end

# Create a design doc with id _design/Cats_stats
design :stats do
  view :by_age, :reduce => :stats
end


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/couchrest/model/designs.rb', line 45

def design(*args, &block)
  opts = prepare_design_options(*args)

  # Store ourselves a copy of this design spec incase any other model inherits.
  (@_design_blocks ||= [ ]) << {:args => args, :block => block}

  mapper = DesignMapper.new(self, opts[:prefix])
  mapper.instance_eval(&block) if block_given?

  # Create an 'all' view if no prefix and one has not been defined already
  mapper.view(:all) if opts[:prefix].nil? and !mapper.design_doc.has_view?(:all)
end

#design_docsObject



86
87
88
# File 'lib/couchrest/model/designs.rb', line 86

def design_docs
  @_design_docs ||= []
end

#inherited(model) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/couchrest/model/designs.rb', line 58

def inherited(model)
  super

  # Go through our design blocks and re-implement them in the child.
  unless @_design_blocks.nil?
    @_design_blocks.each do |row|
      model.design(*row[:args], &row[:block])
    end
  end
end

#paginates_per(val) ⇒ Object

Override the default page pagination value:

class Person < CouchRest::Model::Base
  paginates_per 10
end


75
76
77
# File 'lib/couchrest/model/designs.rb', line 75

def paginates_per(val)
  @_default_per_page = val
end