Rails2 update_without_callbacks monkeypatch - ruby-on-rails-3

I know monkey patches are bad, but I have the following patch for update_without_callbacks for my Rails2 app, but I am having a hard time porting it to rails3 as that method no longer exists in Rails3.
Here is the definition:
def update_without_callbacks(attribute_names = #attributes.keys)
if changed?
update_creating_new_version_row(attribute_names)
update_shared_columns
else
Rails.logger.info("this record unchanged; skipping update")
end
true
end
Please suggest as to how i should go about porting it to Rails3. Thanks.

It should be replaced by:
save(:callbacks => false)

Related

Using Ruby coreaudio gem

I'm creating an audio project as an academic exercise - I want to use Ruby for a non-rails project. I came across the coreaudio gem here: https://github.com/nagachika/ruby-coreaudio and thought it would be useful in playing back the audio I'm creating. However, there are some basic things I'm just not getting about this gem.
1) Is there any documentation? I would be happy to volunteer to contribute to the project by writing documentation but I'm afraid I lack the basic understanding to do so. I looked at the generated documentation here: http://rubydoc.info/gems/coreaudio/ but that's only more mysterious. I would think that would at least show the method prototypes but I only see one method CoreAudio::AudioFile:read. The examples have calls to other methods that don't show up here.
2) What are the available methods / is there an API? The examples have things like CoreAudio.default_input_device.input_buffer and CoreAudio.default_output_device.output_stream, and by copying the examples I have been able to get some sound out through the CoreAudio.default_output_device.output_buffer. default_input_device and default_output_device are instances of CoreAudio::AudioDevice. This is awesome, but I don't know what CoreAudio::AudioDevice is.
3) What is CoreAudio::AudioDevice? Having no more sophisticated tools at my disposal I grepped for the string 'AudioDevice' in the installed gem files. I found a number of instances in the file /ext/coreaudio.m. The code looks like C and I think .m is a file suffix for a certain type of file in an Objective C project. The only place in that file where I see the string 'AudioDevice' where it isn't part of a larger variable name (eg: rb_cAudioDevice) is in the comments, but I'm guessing that this objective C code gets compiled into... I don't know what...
4) What are these Objective C files doing here? Where's the Ruby? Shouldn't there be a file or files written in Ruby somewhere in this project that define the class CoreAudio::AudioDevice? The only .rb file on any appreciable size is lib/coreaudio/audiofile.rb (where the read method is defined, the only method shown in rdoc).
5) What is coreaudio_ext.bundle? The gem installation process created a binary file called coreaudio_ext.bundle which, if I had to guess, would be the result of compiling the objective C files in the project. This file is required in lib/coreaudio/coreaudio.rb, so must be significant to Ruby, but it's a black box to me. I assume it is a black box containing the definition of the ephemeral CoreAudio::AudioDevice. How would I know how to use this other than looking at the examples?
Please help me understand what I'm missing.
Since I asked, I'll attempt to document my answer for any future archaeologists who may puzzle with the same questions. Thanks to Neil Slater, for pointing me in the direction of important resources needed to answer my question(s).
1) Is there any documentation? - not really. The CoreAudio::AudioFile:read function is the only one documented by rdoc because everything else is implemented in a Ruby extension in C, by the mechanism described in detail here: http://media.pragprog.com/titles/ruby3/ext_ruby.pdf. Turns out that this process has been done for a great number of existing C libraries to add their functionality to Ruby.
2) What are the available methods / is there an API? - The extension created is the equivalent of the following: (corrections welcome)
Module CoreAudio
class AudioDevice
attr_reader :devid, :name, :available_sample_rate, :nominal_rate, :input_stream, :output_stream
def initialize(devIdVal, options)
...
end
def actual_rate
...
end
def output_loop(frame)
...
end
def output_buffer(frame)
...
end
def input_buffer(frame)
...
end
end
class AudioStream
attr_reader :channels, :buffer_frame_size
def initialize(devid_val, is_input)
...
end
end
class OutLoop
def []=(index, val)
...
end
def start
...
end
def stop
...
end
end
class AudioBuffer
def start
...
end
def stop
...
end
def dropped_frame
...
end
def reset_dropped_frame
...
end
def space
...
end
end
class OutputBuffer
def <<(nary)
...
end
end
class InputBuffer
def read(num)
...
end
end
def devices
...
end
def default_input_device
...
end
def default_output_device
...
end
def set_default_output_device
...
end
class AudioFile
def initialize
...
end
def close
...
end
def write(data)
...
end
def read_frames(frame_val)
...
end
def rate
...
end
def channels
...
end
def inner_rate
...
end
def inner_channels
...
end
end
end
3) What is CoreAudio::AudioDevice? - I was mistaken about the string "AudioDevice" not appearing on its own as an identifier - it appears in the C code as follows:
rb_cAudioDevice = rb_define_class_under(rb_mCoreAudio, "AudioDevice", rb_cObject);
this is declaring the class AudioDevice inside of module CoreAudio.
4) Where's the Ruby? - this gem is extending Ruby with coreaudio, written in C.
5) What is coreaudio_ext.bundle? this is the compiled C code. It's required by the gem in coreaudio.rb
require "coreaudio/coreaudio_ext"
A .bundle file is the Mac equivalent of a .dll file in Windows. (I don't believe there is a Windows implementation of this gem, as CoreAudio is a Mac-specific technology)

Extend a module in Rails 3

I want to define a function available_translations which lists the translations I have made for my application into the I18n module.
I tried putting the following into the file lib/i18n.rb, but it doesn't work when I try to use it from the rails console:
module I18n
# Return the translations available for this application.
def self.available_translations
languages = []
Dir.glob(Rails.root.to_s + '/config/locales/*.yml') do |filename|
if md = filename.match #^.+/(\w+).yml$#
languages << md[1]
end
end
languages
end
end
Console:
ruby-1.9.2-p290 :003 > require Rails.root.to_s + '/lib/i18n.rb'
=> false
ruby-1.9.2-p290 :004 > I18n.available_translations
NoMethodError: undefined method `available_translations' for I18n:Module
...
Besides solving my concrete problem, I would be very pleased to learn how this whole module thing in Ruby on Rails works because it still confuses me, so I would appreciate links to the docs or source code very much.
Either of these will solve your problem:
move the code to config/initializers/i18n.rb, or
require your file from config/application.rb, or
name your class otherwise (to trigger autoload)
The code in lib/i18n.rb wil not be loaded by autoload since I18n name will be already loaded, so either you load it yourself or change the class name (and file name) so the new name will trigger autoload behavior.
BTW, the I18n.available_locales() method is presented in rails.

testing emails with capybara and delayed_job

inspired by the episode of railscast (http://railscasts.com/episodes/275-how-i-test) i tried to add some request specs to my app.
using delayed_job for sending my emails, i did not found an easy way to test the sending of emails within my capybara test. i tried:
it "emails user when requesting password reset" do
...(some user action that triggers sending an email)...
Delayed::Worker.new.work_off
ActionMailer::Base.deliveries.last.to.should include(user.email)
end
thanks for any hints or help!
Much easier solution: Just create an initializer in config/initializers, e.g. delayed_job_config.rb
Delayed::Worker.delay_jobs = !Rails.env.test?
In your actual test you do not even need to know that delayed_job is deployed to send the mails, which keeps them clean and simple.
well, it should work. i found some misconfigurations in my app.
check you test env. you should set:
Staffomatic::Application.configure do
...
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { :host => "example.com" }
end
i added a line in the mailer macros module from the railscast to work off the Delayed::Job:
module MailerMacros
def last_email
Delayed::Worker.new.work_off
ActionMailer::Base.deliveries.last
end
def reset_email
Delayed::Job.destroy_all
ActionMailer::Base.deliveries = []
end
end
now you can check with:
last_email.to.should include(user.email)
your last email.
pretty easy!
ps. if you have the awesome mail_safe gem installed you should make sure it's not in the test env.!

Record persisting past test in Rails3

I'm working through the railstutorial.org site and seem to have a problem with an integration test. It's suppose to check if a user gets properly created after a form post which works but, on subsequent tests fails because the test db is not getting rolled backed, this causes error because of validating that users can't have same email. Any explanation why the record would persist? If relevant the code in question is from this listing.
Note: I am the author of the Rails Tutorial. The config.cache_classes = false line got added after Peter Cooper reported that it was necessary to get RSpec and Spork to work together on his system. Since I have not found it necessary, and since it seemed to introduce lots of problems (such as those identified in this thread), that line has since been removed. If you use the latest version of the book you shouldn't run into this problem.
Look into using database_cleaner. Your spec helper will contain something like this:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
ActionMailer::Base.deliveries = []
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
Seems that the issue was with the line
config.cache_classes = false
I had set this to false assuming it would make sure not to use stale class data, but it seems to be having the opposite effect among other things. Changing this to true fixed all the weirdness I was having, but I am still on confused as to why. I think it may have something to do with the OS as in the tutorial it was said that for OSX (which I am running) having that line set to true works fine, while other OSes need it set false
While Hartl's tutorial struck me as flawless, perhaps the issue you raise here can be classified as an important ommission.
This here: RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test
and
This here: Rails 3 Tutorial Chapter 11 "Validation failed: Email has already been taken" error
and
This here: config.cache_classes = false messing up rspec tests?
... are all variations on the same problem.
Mike Hartl, if you are out there, you seem a mere one issue away from RoR Tutorial perfection.
Best regards,
Perry

uninitialized constant Twitter::OAuth

I'm struggling to get my app to display a timeline of feeds from my app. So far I've used the oauth-plugin, oauth and twitter gems (for rails3) to get it authorised. This has worked just fine.
Now I'm struggling when I try and connect.
I end up with an error:
uninitialized constant Twitter::OAuth
Have checked I don't have another action calling twitter (as in another post here). But so far, no luck.
Hope someone can help!
Edit -
I forgot to mention I'm using Devise to authenticate my users. Have tried inserting:
require 'twitter'
But still no success..
-- EDIT TWO --
Found a solution on the twitter gem git site about depreciating this in version 1.0.
I've now replaced the code in my twitter_token.rb file with:
def client
unless #client
#twitter_oauth=Twitter::Client.new(:TwitterToken.consumer.key,:TwitterToken.consumer.secret)
#twitter_oauth.authorize_from_access(token,secret)
#client=Twitter::Base.new(#twitter_oauth)
end
Which gets rid of that error but now leads to another :(
undefined method `consumer' for :TwitterToken:Symbol
I have also tried this:
def client
unless #client
#twitter_oauth=Twitter::Client.new(:oauth_token =>'TwitterToken.consumer.key', :oauth_token_secret=>'TwitterToken.consumer.secret')
#twitter_oauth.authorize_from_access token,secret
#client=Twitter::Base.new(#twitter_oauth)
end
Which gives the following error:
undefined method `authorize_from_access' for #<Twitter::Client:0x00000102da1530>
Any ideas? I'm going insane!
I'm going to answer my own question here - if it helps one person, it's worth it considering I lost three days to it.
Using the latest twitter gem, devise and oauth-plugin. I was seeing a lot of errors. The latest twitter_token controller on the oauth-plugin site does not work, even though it's been updated for a recent twitter gem..
In the end, I deleted my entire twitter_token.rb file and started again:
require 'twitter'
class TwitterToken < ConsumerToken
TWITTER_SETTINGS={:site=>"http://api.twitter.com", :request_endpoint => 'http://api.twitter.com',}
def self.consumer
#consumer||=OAuth::Consumer.new credentials[:key],credentials[:secret],TWITTER_SETTINGS
end
def client
Twitter.configure do |config|
config.consumer_key = TwitterToken.consumer.key
config.consumer_secret = TwitterToken.consumer.secret
config.oauth_token = token
config.oauth_token_secret = secret
end
#client ||= Twitter::Client.new
end
end
You can then update twitter using something like this:
<%= current_user.twitter_token.client.update("At last it's working!") %>
Also, make sure you're using the rails3 branch of the oauth-plugin..