Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow js views #139

Open
wants to merge 9 commits into
base: 1.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ GEM
railties (>= 3.0.0)
fakeweb (1.3.0)
hike (1.2.3)
httparty (0.14.0)
httparty (0.15.6)
multi_xml (>= 0.5.2)
i18n (0.6.9)
mail (2.5.4)
Expand Down Expand Up @@ -116,4 +116,4 @@ DEPENDENCIES
turn!

BUNDLED WITH
1.14.6
1.12.0
2 changes: 1 addition & 1 deletion lib/dolly/bulk_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def docs

def save
return if docs.empty?
self.response = JSON::parse self.database.post(DOC_NAME, json_payload)
self.response = self.database.post(DOC_NAME, json_payload)
build_errors
update_revs
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dolly/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def rows= ary
end

def load
parsed = JSON::parse json
parsed = json.is_a?(String) ? JSON.parse(json) : json
self.rows = parsed['rows']
end

Expand Down
11 changes: 7 additions & 4 deletions lib/dolly/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def save options = {}
self.doc['_id'] = self.class.next_id if self.doc['_id'].blank?
set_created_at if timestamps[self.class.name]
set_updated_at if timestamps[self.class.name]
response = database.put(id_as_resource, self.doc.to_json)
obj = JSON::parse response.parsed_response
obj = database.put(id_as_resource, self.doc.to_json)
doc['_rev'] = obj['rev'] if obj['rev']
obj['ok']
end
Expand All @@ -77,8 +76,7 @@ def save!
def destroy hard = true
if hard
q = id_as_resource + "?rev=#{rev}"
response = database.delete(q)
JSON::parse response.parsed_response
database.delete(q)
else
self.doc['_deleted'] = true
self.save
Expand All @@ -104,6 +102,11 @@ def from_json string
self
end

def from_response response
self.rows = response['rows']
self
end

def database
self.class.database
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dolly/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Property
attr_accessor :name
attr_reader :class_name, :default

CANT_CLONE = [NilClass, TrueClass, FalseClass, Fixnum].freeze
CANT_CLONE = [NilClass, TrueClass, FalseClass, Integer].freeze

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that you should still use fixnum

> 1.frozen?
=> true
[2] pry(main)> 1.5.frozen?
=> true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> 1.5.clone
TypeError: can't clone Float

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it actually looks like Float is not a fixnum, so that should be added too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixnum is depracated in rails > 5.0 we need to remove it, and yeah we need to add float
I dont thing integer is needed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this change in rails 5?

> 1.clone
TypeError: can't clone Fixnum

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it change > 1.clone works on rails 5 with no error... I dont think this particular change affect us that much we can handle it and make it backwards compatible

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change does affect us a lot. any default property that is an integer or a float would be affected

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is important for default properties to work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you don't think we can handle it in a way is compatible in both Tails 4 and 5?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is a change to the inheritance models of the underlying rails/ruby code, I dont think so. Feel free to prove me wrong. I feel like this is a change that will not be compatable with rails 4 if Fixnum does not exist


def initialize opts = {}
@class_name = opts.delete(:class_name) if opts.present?
Expand Down
8 changes: 4 additions & 4 deletions lib/dolly/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def find *keys
if keys.count > 1
build_collection( query_hash )
else
self.new.from_json( database.all_docs(query_hash).parsed_response )
self.new.from_response(database.all_docs(query_hash))
end
rescue NoMethodError => err
if err.message == "undefined method `[]' for nil:NilClass"
Expand Down Expand Up @@ -48,12 +48,12 @@ def last limit = 1

def build_collection q
res = database.all_docs(q)
Collection.new res.parsed_response, name_for_class
Collection.new res, name_for_class
end

def find_with doc, view_name, opts = {}
res = view "_design/#{doc}/_view/#{view_name}", opts
Collection.new res.parsed_response, name_for_class
Collection.new res, name_for_class
end

#TODO: new implementation for collection returning
Expand All @@ -72,7 +72,7 @@ def view doc, options = {}
end

def raw_view doc, view, opts = {}
JSON.parse database.get "_design/#{doc}/_view/#{view}", opts
database.get("_design/#{doc}/_view/#{view}", opts)
end

end
Expand Down
5 changes: 3 additions & 2 deletions lib/dolly/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def all_docs data = {}
def request method, resource, data = nil
data ||= {}
data.merge!(basic_auth: auth_info) if auth_info.present?
headers = { 'Content-Type' => 'application/json' }
headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
headers.merge! data[:headers] if data[:headers]
response = self.class.send method, resource, data.merge(headers: headers)
log_request(resource, response.code) if Dolly.log_requests?
Expand All @@ -76,7 +76,8 @@ def request method, resource, data = nil
elsif (400..600).include? response.code
raise Dolly::ServerError.new( response )
else
response
body = response.parsed_response
body.is_a?(String) ? JSON.parse(body) : body
end
end

Expand Down
16 changes: 13 additions & 3 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ namespace :db do
task design: :environment do
design_dir = Rails.root.join 'db', 'designs'
files = Dir.glob File.join design_dir, '**', '*.coffee'
files += Dir.glob File.join design_dir, '**', '*.js'

data = {}

files.each do |filename|
parts = filename[design_dir.to_s.length+1..-1].split '/'
design_doc_name = parts.count == 1 ? Dolly::Document.design_doc : "_design/#{parts.first}"

name, key = File.basename(filename).sub(/.coffee/i, '').split(/\./)
name, key = File.basename(filename).sub(/.coffee|.js/i, '').split(/\./)
language = File.basename(filename) =~ /.js/ ? 'javascript' : 'coffeescript'

key ||= 'map'
source = File.read filename

vd = data[design_doc_name] ||= { 'views' => {}, 'filters' => {}, 'lists' => {}, 'lib' => {} }

vd['language'] ||= language

if data[design_doc_name]['language'] != language
raise 'Design document can only have one view language'
end

if key == 'filter'
vd['filters'][name] = source
elsif key == 'lists'
Expand All @@ -35,10 +45,10 @@ namespace :db do
end

data.each do |design_doc_name, view_doc|
view_doc.merge!( '_id' => design_doc_name, 'language' => 'coffeescript')
view_doc.merge!('_id' => design_doc_name)

begin
hash_doc = JSON::parse Dolly::Document.database.get(view_doc["_id"]).parsed_response
hash_doc = Dolly::Document.database.get(view_doc["_id"])

rev = hash_doc.delete('_rev')

Expand Down