I started this series nearly two weeks ago with a pretty solid understanding of both Devise and SimpleDB (via AWS::Record::Model in aws-sdk) as individual pieces. In the course of getting them to work together I’ve learned a lot about the internals of both projects (also ActiveModel and ActiveRecord), created three new gems, and had a few pull requests accepted into the aws-sdk gem. In this post I’m going to do some more refactoring of the ongoing SimpleDevise project to correct an early mis-step and to remove some methods that are now provided by aws-sdk
.
Gemfile
First, we need to update the aws-sdk
gem to point to the current project on Github. Once version 1.8.1 has been released here, we’ll be able to go back to a regular gem release. So, find the line for aws-sdk
in the Gemfile
and change it to look like this:
gem "aws-sdk", :git => "git://github.com/aws/aws-sdk-ruby.git"
This will pick up 3 updates that provide functionality that we need.
#create
and #create!
methods to AWS::Record::Model
:allow_blank
option to some validations:validate
option to the #save
method of AWS::Record::Model
We also need to add a new gem
gem "simple_date_fix", "~> 0.0.4"
SimpleDateFix is based on work done by daniel-nelson to get queries and sorting to work correctly on datetime_attrs
in AWS::Record::Model
. Thanks, Daniel!
Now, we just need to install the updates, so run:
$ bundle install
Now we can start the refactoring.
One of the steps taken in part 1 was just the wrong course of action. After coming to understand the problem more thoroughly I ended up making pull requests to the aws-sdk
gem to correct the problem.
First, open config/initializers/aws.rb
and remove these lines:
AWS::Record::FormatValidator::ACCEPTED_OPTIONS << :allow_blank
AWS::Record::LengthValidator::ACCEPTED_OPTIONS << :allow_blank
It turns out that just faking out these validators doesn’t really cut it. This pull request added proper handling for the :allow_blank
option, so now Devise will be happy AND things will work like it expects them to.
Since we’re using a new version of aws-sdk
that contains some new methods we can get rid of our own custom version of those methods.
Open up the User
model in app/models/user.rb
and remove the following lines:
# Some methods to fake out Devise def save opts = {} if valid?(opts) persisted? ? update : create clear_changes! true else false end end
def valid? opts = {} opts = {} if opts.nil? opts = {:validate => true}.merge(opts) run_validations if opts[:validate] errors.empty? end
Both of those methods were there to allow Devise to skip validations during a #save
operation, and the ability to skip validations was added to aws-sdk
by this pull request.
That’s it! This was just a small bit of refactoring to get things onto the right track. We got rid of a dirty hack that wasn’t doing us any real good and we got to remove some code that is now a part of the aws-sdk
library. It’s always nice to be able to trim your codebase by delegating functionality where it belongs. In the next part I plan to add some some testing to the app.