I am trying to interface with the YouTube player API. This works fine. However, the API lets you define callbacks that are fired whenever, for instance, the video has finished playing.
I would like to call a ModelController action at that time. How can I access actions on my ModelController from within native JS? Opal.Volt.??? ?
[question copied from gitter]
on the accessing the model controller. What you want to do is set the callback up from inside of the controller. In opal you can use a proc (not a block) as a callback (since it can be used in place of a JS function) Then inside of the proc, call whatever you want on the ModelController. You can also convert a method into
module Main
class MainController < Volt::ModelController
def index
callback = proc {|*args| some_method(*args) }
`youtube.someHandler(callback);`
end
end
end
Related
I have a controller with several actions. Many follow this pattern:
def favorites
#favorites = Favorite.where(organization_id: #resource.id).page(params[:page]).per(50)
end
It's not just favorites, but there's also downloads, searches, lists etc and they're all so similar that I wanted to create a method that I could call in a before_filter. Something like this:
def set_instance_variable
subject = __method__
class = __method__.singularize.constantize
instance_variable = self.class.instance_variable_set("##{subject}", "#{class}.where(organization_id: #resource.id).page(params[:page]).per(50)")
end
The syntax might be a little off here, but I know this won't work because __method__ will always be set_instance_variable and not the parent method where it is called.
Is there a way to dynamically set instance variables based on the method that defines them? Is this example above even on the right track?
I like the way the CanCan library handles this problem. With CanCan, you call a class method at the top of your controller:
load_resource
CanCan then looks at:
the action you're in to determine whether you want a collection or singular resource,
the name of the controller to determine the class to load
authorization rules to add scopes like your organization_id restriction (cancan is also a library for defining these)
I think pagination and resource loading are separate things, and you shouldn't put them in the same method. I'd shoot for an interface like this:
class FavoritesController
load_resource
paginate_resource only: [:index]
def show
# #favorite loaded here
end
def index
# #favorites loaded and paginated here
end
end
https://github.com/CanCanCommunity/cancancan/blob/develop/lib/cancan/controller_resource.rb#L29
If it makes more sense in your application to have non-restful resources, then you can't re-use the convention-based thing cancan is and instead have to define your own function. Consider something like this:
def favorites
#favorites = load_resource Favorite
end
private
def load_resource(klass)
klass.where(organization_id: #resource.id).page(params[:page]).per(50)
end
I have a class API that pulls objects from a third party API and builds them into objects that are subclasses of type APIObject. APIObject subclasses match the object names from the API that I'm pulling from:
User < APIObject
Account < APIObject
I would like to define a class method in APIObject that allows me to pull objects using standard Rails accessors:
user = User.find id
I would like the method to translate this call into an API call like this:
API::User::findById id
I would like to access the name of the APIObject subclass (User) using self.class.name and use that to call the constant (API::User), but I know API::self.class.name won't work. I could rewrite this method over and over again for every subclass, but it seems like this should be possible without doing that. Suggestions?
I think you’re looking for const_get. Perhaps something like:
def self.find(id)
API.const_get(self.name).find_by_id(id)
end
(note you only need self.name, since this is already in the context of the class, and self.class.name will just be Class).
Is there a way to get a true proc from a method in Ruby?
An UnboundMethod obtained via instance_method does not fit the bill because I can only bind it to an object of the class that declared the method. I can't reinterpret self inside the method body the way I could in a proc (using instance_exec).
Similarly, a Method obtained via method is not okay, because self is bound to the receiver of method and I cannot change it.
Edit (Clarification):
What I'm trying to do is to take a method defined in one class and transfer it to another class. This means I need to be able to reinterpret the meaning of self within the method. For procs, this is possible via instance_exec and instance_eval, but not for methods.
Why I am trying to move methods from one class to another? Long story short, to implement a form of namespacing, as I am most displeased with the visibility control provided by Ruby (there is no way to hide a module member to an including class). This is however far beyond the scope of this question.
Maybe to_proc from Method can help you:
class A
def test
puts 'this is a test'
end
end
m = A.new.method(:test)
m.to_proc.call #=> this is a test
UPDATE: Just an idea
By using sourcify gem convert proc from first object to source, and then evaulate it in the context of second object
first sorry for my poor english...I've a doubt..I'm reading the FXRuby for the pragmatic programmer..and I saw this code
require 'fox16'
include Fox
class HelloWindow < FXMainWindow
def initialize(app)
super(app, "Hello, World!" , :width => 200, :height => 100)
end
def create
super
show(PLACEMENT_SCREEN)
end
end
app = FXApp.new
HelloWindow.new(app)
app.create
app.run
It's a very basic example...actually It's he first example..but I'm so noob than I don't understand it:
app is a FXAPP object..
now I create a HelloWindow object and pass my FXApp object named "app"
so far so good
but now...in the book write app.create
I'm calling the "create" method inside FXApp class...or not?..
why when I call app.create..ruby call the create method inside HelloWindow?..app is a very different object than HelloWindow class and I could can call an anscestor method (like when I use super) but not at the inverse way...
why they don't call it something like this
helloobject=HelloWindow.new(app)
helloobject.create
this way I calling the create method inside HelloWindows class..and it is descendent from FXMainWindows
I hope than you can understand (sorry for my bad english) and can help me
thanks so much
I don't know anything about FXRuby, but I answer your questions about the Ruby side of things.
When Ruby executes app.create, it will call the create method inside the FXApp class because app's type is FXApp (assuming that there is no create method defined for the singleton class of app).
When you call app.create, there is probably some code in the FXApp class to calls create on all of the windows in the app, so that's how your window's create function gets called. If you want to really find out how your window's create function is being called, try adding raise "hello" to it and see if you get a backtrace of the exception.
I don't really know the answer to your last question because it has to do with the design of the FXRuby library. But conceptually it seems like calling app.create and window.create are very different things. If you want to run the app, you should create it first. Simply creating one window isn't good enough.
I switched to Ruby from PHP, and have yet to understand a curious Ruby class behavior where methods are executed outside of the class method definitions (see example below). In PHP, when we wanted to execute anything on class init, we would put it in the constructor method.
Ruby example (Rails):
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
end
Am I correct in understanding that belongs_to will be executed on instantiation? And is belongs_to a class method inherited from ActiveRecord?
Thanks!
In Ruby, everything is executable code. Or, to put it another way: everything is a script. There is no such thing as a "class declaration" or something like that.
Any code that sits in a file, without being inside anything else like a method body, a class body, a module body or a block body, is executed when that file is loaded (or required or require_relatived). This is called a script body.
Any code that sits inside a class or module body is executed when that class or module is created. (This is the case you are referring to.)
The boring part: any code that sits inside a method body is executed when that method is called, or more precisely, when that method is invoked in response to receiving a message with the same name as the method. (Duh.)
Any code that sits inside a block body is executed when that block is yielded to.
Since a class definition is just a script, this means that it can contain any sort of code you want, including method calls:
class Foo
attr_accessor :bar # Yes, attr_accessor is just a method like any other
private # I *bet* you didn't know *that* was a method, too, did you?
end
or conditionals:
class Bar
if blah # e.g. check if the OS is Windows
def foo
# one way
end
else
def foo
# a different way
end
end
end
Yes, it's a class method from ActiveRecord. The method will execute when the class itself is created, not when an instance of it is created.
Yes, that's correct. See also this question.