Module: Friendly::Document::ClassMethods

Defined in:
lib/friendly/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#association_setObject



154
155
156
# File 'lib/friendly/document.rb', line 154

def association_set
  @association_set ||= Associations::Set.new(self)
end

#collection_klassObject



49
50
51
# File 'lib/friendly/document.rb', line 49

def collection_klass
  @collection_klass ||= WillPaginate::Collection
end

#query_klassObject



45
46
47
# File 'lib/friendly/document.rb', line 45

def query_klass
  @query_klass ||= Query
end

#scope_proxyObject



101
102
103
# File 'lib/friendly/document.rb', line 101

def scope_proxy
  @scope_proxy ||= ScopeProxy.new(self)
end

#storage_proxyObject



41
42
43
# File 'lib/friendly/document.rb', line 41

def storage_proxy
  @storage_proxy ||= StorageProxy.new(self)
end

#table_nameObject



97
98
99
# File 'lib/friendly/document.rb', line 97

def table_name
  @table_name ||= name.pluralize.underscore
end

Instance Method Details

#all(query) ⇒ Object



70
71
72
# File 'lib/friendly/document.rb', line 70

def all(query)
  storage_proxy.all(query(query))
end

#attribute(name, type = nil, options = {}) ⇒ Object



37
38
39
# File 'lib/friendly/document.rb', line 37

def attribute(name, type = nil, options = {})
  attributes[name] = Attribute.new(self, name, type, options)
end

#attributesObject



62
63
64
# File 'lib/friendly/document.rb', line 62

def attributes
  @attributes ||= {}
end

#caches_by(*fields) ⇒ Object



57
58
59
60
# File 'lib/friendly/document.rb', line 57

def caches_by(*fields)
  options = fields.last.is_a?(Hash) ? fields.pop : {}
  storage_proxy.cache(fields, options)
end

#count(conditions) ⇒ Object



80
81
82
# File 'lib/friendly/document.rb', line 80

def count(conditions)
  storage_proxy.count(query(conditions))
end

#create(attributes = {}) ⇒ Object



91
92
93
94
95
# File 'lib/friendly/document.rb', line 91

def create(attributes = {})
  doc = new(attributes)
  doc.save
  doc
end

#create_tables!Object



33
34
35
# File 'lib/friendly/document.rb', line 33

def create_tables!
  storage_proxy.create_tables!
end

#find(id) ⇒ Object

Raises:



74
75
76
77
78
# File 'lib/friendly/document.rb', line 74

def find(id)
  doc = first(:id => id)
  raise RecordNotFound, "Couldn't find #{name}/#{id}" if doc.nil?
  doc
end

#first(query) ⇒ Object



66
67
68
# File 'lib/friendly/document.rb', line 66

def first(query)
  storage_proxy.first(query(query))
end

#has_many(name, options = {}) ⇒ Object

Add a has_many association.

e.g.

class Post
  attribute :user_id, Friendly::UUID
  indexes   :user_id
end

class User
  has_many :posts
end

@user = User.create
@post = @user.posts.create
@user.posts.all == [@post] # => true

_Note: Make sure that the target model is indexed on the foreign key. If it isn’t, querying the association will raise Friendly::MissingIndex._

Friendly defaults the foreign key to class_name_id just like ActiveRecord. It also converts the name of the association to the name of the target class just like ActiveRecord does.

The biggest difference in semantics between Friendly’s has_many and active_record’s is that Friendly’s just returns a Friendly::Scope object. If you want all the associated objects, you have to call #all to get them. You can also use any other Friendly::Scope method.

Parameters:

  • name (Symbol)

    The name of the association and plural name of the target class.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :class_name (String)

    The name of the target class of this association if it is different than the name would imply.

  • :foreign_key (Symbol)

    Override the foreign key.



186
187
188
# File 'lib/friendly/document.rb', line 186

def has_many(name, options = {})
  association_set.add(name, options)
end

#has_named_scope?(name) ⇒ Boolean

Returns boolean based on whether the Document has a scope by a particular name.

Parameters:

  • name (Symbol)

    The name of the scope in question.

Returns:



137
138
139
# File 'lib/friendly/document.rb', line 137

def has_named_scope?(name)
  scope_proxy.has_named_scope?(name)
end

#indexes(*args) ⇒ Object



53
54
55
# File 'lib/friendly/document.rb', line 53

def indexes(*args)
  storage_proxy.add(args)
end

#named_scope(name, parameters) ⇒ Object

Add a named scope to this Document.

e.g.

class Post
  indexes     :created_at
  named_scope :recent, :order! => :created_at.desc
end

Then, you can access the recent posts with:

Post.recent.all

…or…

Post.recent.first

Both #all and #first also take additional parameters:

Post.recent.all(:author_id => @author.id)

Scopes are also chainable. See the README or Friendly::Scope docs for details.

Parameters:

  • name (Symbol)

    the name of the scope.

  • parameters (Hash)

    the query that this named scope will perform.



129
130
131
# File 'lib/friendly/document.rb', line 129

def named_scope(name, parameters)
  scope_proxy.add_named(name, parameters)
end

#paginate(conditions) ⇒ Object



84
85
86
87
88
89
# File 'lib/friendly/document.rb', line 84

def paginate(conditions)
  query      = query(conditions)
  count      = count(query)
  collection = collection_klass.new(query.page, query.per_page, count)
  collection.replace(all(query))
end

#scope(parameters) ⇒ Object

Create an ad hoc scope on this Document.

e.g.

scope = Post.scope(:order! => :created_at)
scope.all # => [#<Post>, #<Post>]

Parameters:

  • parameters (Hash)

    the query parameters to create the scope with.



150
151
152
# File 'lib/friendly/document.rb', line 150

def scope(parameters)
  scope_proxy.ad_hoc(parameters)
end