Module: Friendly::Document::ClassMethods
- Defined in:
- lib/friendly/document.rb
Instance Attribute Summary collapse
- #association_set ⇒ Object
- #collection_klass ⇒ Object
- #query_klass ⇒ Object
- #scope_proxy ⇒ Object
- #storage_proxy ⇒ Object
- #table_name ⇒ Object
Instance Method Summary collapse
- #all(query) ⇒ Object
- #attribute(name, type = nil, options = {}) ⇒ Object
- #attributes ⇒ Object
- #caches_by(*fields) ⇒ Object
- #count(conditions) ⇒ Object
- #create(attributes = {}) ⇒ Object
- #create_tables! ⇒ Object
- #find(id) ⇒ Object
- #first(query) ⇒ Object
-
#has_many(name, options = {}) ⇒ Object
Add a has_many association.
-
#has_named_scope?(name) ⇒ Boolean
Returns boolean based on whether the Document has a scope by a particular name.
- #indexes(*args) ⇒ Object
-
#named_scope(name, parameters) ⇒ Object
Add a named scope to this Document.
- #paginate(conditions) ⇒ Object
-
#scope(parameters) ⇒ Object
Create an ad hoc scope on this Document.
Instance Attribute Details
#association_set ⇒ Object
154 155 156 |
# File 'lib/friendly/document.rb', line 154 def association_set @association_set ||= Associations::Set.new(self) end |
#collection_klass ⇒ Object
49 50 51 |
# File 'lib/friendly/document.rb', line 49 def collection_klass @collection_klass ||= WillPaginate::Collection end |
#query_klass ⇒ Object
45 46 47 |
# File 'lib/friendly/document.rb', line 45 def query_klass @query_klass ||= Query end |
#scope_proxy ⇒ Object
101 102 103 |
# File 'lib/friendly/document.rb', line 101 def scope_proxy @scope_proxy ||= ScopeProxy.new(self) end |
#storage_proxy ⇒ Object
41 42 43 |
# File 'lib/friendly/document.rb', line 41 def storage_proxy @storage_proxy ||= StorageProxy.new(self) end |
#table_name ⇒ Object
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, = {}) attributes[name] = Attribute.new(self, name, type, ) end |
#attributes ⇒ Object
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) = fields.last.is_a?(Hash) ? fields.pop : {} storage_proxy.cache(fields, ) 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
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.
186 187 188 |
# File 'lib/friendly/document.rb', line 186 def has_many(name, = {}) association_set.add(name, ) end |
#has_named_scope?(name) ⇒ Boolean
Returns boolean based on whether the Document has a scope by a particular name.
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.
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>]
150 151 152 |
# File 'lib/friendly/document.rb', line 150 def scope(parameters) scope_proxy.ad_hoc(parameters) end |