georgepalmer-couch_foo 0.7.8 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 7
4
- :patch: 8
4
+ :patch: 9
@@ -173,21 +173,23 @@ module CouchFoo
173
173
  # possible to find the oldest 5 users in the system if :created_at isn't exposed in the key. As
174
174
  # such it is possible to set the key to use with a view directly. For example:
175
175
  #
176
- # Person.find(:first, :use_key => :created_at) # Finds earliest person as results sorted on key
177
176
  # Person.find(:all, :use_key => [:name, :category], :conditions => {:category => "Article"}, :limit => 50) # Finds 50 people with a category of "Article" sorted by name
178
177
  #
179
- # Note in the second case that we must use the condition keys in the :use_key array. Both of
180
- # these will get the desired results but at the expense of creating a new index. For more complex
181
- # queries it is also possible to specify your own map and reduce functions using the CouchFoo#view
182
- # call. See the CouchDB view documentation and the CouchFoo#view documentation for more on this
178
+ # We must use the condition keys in the :use_key array as we want to restrict the results on this.
179
+ # It should be noted that the query will get the desired results but at the expense of creating a
180
+ # new index so shouldn't be used excessively. For more complex queries it is also possible to
181
+ # specify your own map and reduce functions using the CouchFoo#view call. See the CouchDB view
182
+ # documentation and the CouchFoo#view documentation for more on this.
183
183
  #
184
184
  # Using relational databases with incrementing keys we have become accustom to adding a new record
185
185
  # and then using find(:last) to retrieve it. As each document in CouchDB has a unique identifier
186
- # this is no longer the case. This is particularly important when creating interfaces as it is
187
- # normal to add items to the bottom of lists and expect on reload for the order to be maintained.
188
- # This will not happen with CouchDB. As such it is recommend to use the CouchFoo#default_sort
189
- # macro that applies a default sort order to the model each time it's retrieved from the database.
190
- # This way you can set default_sort :created_at and not worry about hitting the problem again.
186
+ # this may no longer the case. This is particularly important when creating user interfaces as it
187
+ # is normal to add items to the bottom of lists and expect on reload for the order to be maintained.
188
+ # In couch_foo items are sorted by the :created_at property if it is available and there are no
189
+ # conditions on the query - eg User.all, User.first As this is a minor use case it is recommended
190
+ # to use the CouchFoo#default_sort macro that applies a default sort order to the model each time
191
+ # it's retrieved from the database. This way you can set default_sort :created_at and not worry
192
+ # about hitting the problem again.
191
193
  #
192
194
  # With CouchDB the price to pay for inserting data into an indexed view isn't paid at insertion
193
195
  # time like MySQL and friends, but at the point of next retrieving that view (although it's
@@ -530,12 +532,12 @@ module CouchFoo
530
532
  # efficiency rather than O(1) (as with ActiveRecord) if using CouchDB<0.9
531
533
  # * Find first - This will return the first record matched by the options used. These options can either be
532
534
  # specific conditions or merely an order. If no record can be matched, +nil+ is returned. Use
533
- # <tt>Model.find(:first, *args)</tt> or its shortcut <tt>Model.first(*args)</tt>. Be aware this will return
534
- # the first document by UUID and this isn't always what you expect even when using default_sort (see note above)
535
+ # <tt>Model.find(:first, *args)</tt> or its shortcut <tt>Model.first(*args)</tt>. It is recommended you
536
+ # use CouchFoo#default_sort on the model if you wish to use this with ordering.
535
537
  # * Find last - This will return the last record matched by the options used. These options can either be
536
538
  # specific conditions or merely an order. If no record can be matched, +nil+ is returned. Use
537
- # <tt>Model.find(:last, *args)</tt> or its shortcut <tt>Model.last(*args)</tt>. Be aware this will return
538
- # the last document by UUID and this isn't always what you expect even when using default_sort (see note above)
539
+ # <tt>Model.find(:last, *args)</tt> or its shortcut <tt>Model.last(*args)</tt>. It is recommended you
540
+ # use CouchFoo#default_sort on the model if you wish to use this with ordering.
539
541
  # * Find all - This will return all the records matched by the options used.
540
542
  # If no records are found, an empty array is returned. Use
541
543
  # <tt>Model.find(:all, *args)</tt> or its shortcut <tt>Model.all(*args)</tt>.
@@ -2007,7 +2009,7 @@ module CouchFoo
2007
2009
  attribs["_rev"] = nil
2008
2010
  attribs["ruby_class"] = self.class.document_class_name
2009
2011
  self.class.properties.inject(attribs) do |attributes, property|
2010
- attributes[property.name.to_s] = property.default
2012
+ attributes[property.name.to_s] = convert_to_json(property.default, property.type)
2011
2013
  attributes
2012
2014
  end
2013
2015
  end
@@ -19,7 +19,7 @@ module CouchFoo
19
19
  reduce_function = nil
20
20
  if database.version > 0.8
21
21
  reduce_function = count_documents_function
22
- search_values[:reduce] = false
22
+ options[:reduce] = false
23
23
  end
24
24
 
25
25
  generic_view(get_view_name(search_fields), find_by_function(search_fields), reduce_function, options)
@@ -89,8 +89,7 @@ module CouchFoo
89
89
 
90
90
  # Returns a name for a view
91
91
  def get_view_name(search_fields, prefix = "find")
92
- ending = search_fields.empty? ? "id" : search_fields.join('_and_')
93
- prefix + "_by_" + ending
92
+ prefix + "_by_" + search_fields.join('_and_')
94
93
  end
95
94
 
96
95
  # Submit query to database
@@ -126,6 +125,19 @@ module CouchFoo
126
125
  else
127
126
  search_fields = options[:conditions].to_a.sort_by{|f| f.first.to_s}.map(&:first)
128
127
  end
128
+
129
+ # If no seach field fall back on :created_at or :id
130
+ if search_fields.empty?
131
+ if property_names.include?(:created_at)
132
+ search_fields << :created_at
133
+ else
134
+ search_fields << :_id
135
+ end
136
+ end
137
+
138
+ # Deal with missing underscore automatically
139
+ search_fields << :_id if search_fields.delete(:id)
140
+ search_fields << :_rev if search_fields.delete(:rev)
129
141
  search_fields
130
142
  end
131
143
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: georgepalmer-couch_foo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.8
4
+ version: 0.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Palmer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-09 00:00:00 -08:00
12
+ date: 2009-02-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency