Class: WingedCouch::Rails::ViewHelpers::ClientSide

Inherits:
Object
  • Object
show all
Defined in:
lib/winged_couch/rails.rb

Overview

Class with view helpers

Instance Method Summary collapse

Instance Method Details

#client_side(builder) ⇒ String

Trasnforms query to CouchDB database to client-side query

Allows you to generate queries on server, but perform on client-side

Examples:

# In your controller:
@profiles = Profile.build(view: "by_name").limit(10).skip(20)

# In your view
var profilesQuery = <%= winged_couch.client_side(@profiles) %>
# => var profilesView = (new WingedCouch.View(Profile, 'by_name')).withParam('limit', '10').withParam('skip', '10');

25
26
27
28
29
30
31
32
33
# File 'lib/winged_couch/rails.rb', line 25

def client_side(builder)
  view_name = builder.path.split("/").last
  model_name = builder.model.name
  result = "(new WingedCouch.View(#{model_name}, '#{view_name}'))"
  builder.params.each do |key, value|
    result += ".withParam('#{key}', '#{value}')"
  end
  result.html_safe
end

#generate_model(klass, client_model_name = klass.name.demodulize) ⇒ String

Generates javascript class for specified server-side model

Examples:

# In your view:
<%= winged_couch.generate_model Profile %>
# => "WingedCouch.Model.build('Profile', 'profile');"
# Now you can use class `Profile` in javascript

# You can specify name of client-side model:
<%= winged_couch.generate_model Profile, 'MyModel' %>
# => "WingedCouch.Model.build('MyModel', 'profile');"
# => In this case, you javascript class is `MyModel`

53
54
55
# File 'lib/winged_couch/rails.rb', line 53

def generate_model(klass, client_model_name = klass.name.demodulize)
  "WingedCouch.Model.build('#{client_model_name}', '#{klass.database.name}');".html_safe
end