Running any controller spec would error with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
After searching for a bit I found the answer on a github issue, tried to find it again and could not otherwise I would link it here.
My co-worker ran into the same issue so I figured it was time to write a blog post.
The fix: use rspec-rails 4 which is currently in development, modify your Gemfile like so:
1 2 3 4 5 6 7 8 9 |
|
and then bundle, re-run the specs
1 2 3 4 |
|
Life is good again!
]]>I have found the following technique useful when debugging json structures generated from ruby hashes.
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Sometimes it would take re-running the build 3 or 4 times before going green.
Each failure was always due to installing via Git URL.
Adding ~/.npm/_git-remotes/
to the cache_directories
of the circle configuration fixed the majority of these issues:
1 2 3 4 5 6 7 8 9 10 11 |
|
The rspec upgrade was much easier than I had anticipated thanks to the transpec gem.
1 2 3 4 |
|
Only a few minor tweaks were needed to my specs after the migration – the transpec is brilliant!
]]>When the environment is production, the addon will automatically fingerprint your js, css, png, jpg, and gif assets …
No fingerprinting occurs in the development environment. These are great defaults, but what if you have a staging environment and want to fingerprint those assets? You can override the fingerprint setting when the ember application is created.
1 2 3 4 5 |
|
Now all environments will fingerprint assets, but we really don’t want that in development. Since the ember application has not booted up yet we do not have access to the configuration object directly – lucky for us it is available on the process
object before the ember application is created.
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
This policy gives full access to certificate management and nothing else.
bundle install
and received the following error:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
What? rbenv should not be prompting me to use sudo, it was trying to install gems on the system version of ruby. Running rbenv local
returned 2.1.4
as expected, what the heck was going on?
gem list | grep bundler
returned nothing – doh! I didn’t have bundler installed via rbenv so it was falling back to the system ruby. I was not expecting it to use a different version of bundler.
1 2 3 4 |
|
And all was right with the world again!
]]>From the app store install XCode
xcode-select --install
1 2 3 4 5 6 7 |
|
Modify the soloistrc
file if desired; for me that means adding ‘sourcetree’, ‘hipchat’ and a few others to the casks section.
The full list of available casks are here: https://github.com/pivotal-sprout/sprout-osx-apps/tree/master/recipes
You probably want to change the ‘Energy Saver’ settings to never while the installs are running.
bundle exec soloist
Enter your password after prompted and then watch the magic happen…
A few hours later, after sprout-wrap is done; time for a few more ‘manual’ installs
then Encrypt your hard drive using FileVault2
Done! :)
NOTE: if you install sprout-wrap with defaults it will configure many os-x settings which you may or may not like; I personally do like them, especially the ‘Fast Key Repeat Rate’.
This post originally appeared on the private discussion board Ruby Rogues Parley.
]]>Let us say we have an Event
class in our rails application with the following attributes:
1 2 3 4 5 6 |
|
Rails uses the timezone format: Eastern Time (US & Canada)
Javascript (and therefore the moment-timezone library) use the format: America/New_York
Rails can easily convert this for you and return the proper js format in your json:
1 2 3 4 5 6 7 8 |
|
and with moment.js and moment-timezone
1 2 |
|
You will want to ensure that all json is being rendered with UTC
as the configured timezone in rails,
i.e. config.time_zone = 'UTC'
in your application config and/or Time.zone = 'UTC'
in your api controller.
See also
]]>Add this code to a shell script and it will lint all files that have been staged. Note it uses the autofix rubocop option, optionally you could run it on non-staged files without the autofix flag.
1
|
|
Don’t forget to git add .
after rubocop finds/fixes any code style violations in your staged files.
Expected undefined to equal 7.
We recently started using Protractor for end to end testing of an angular application. Protractor returns promises when locating elements on the page, in some cases you may need to do your assertions inside of callback functions.
For example:
1 2 3 4 5 6 7 8 9 10 |
|
You can use the elementexplorer to debug your locators, but be aware that even non-matched elements will return an ‘Element Finder’ object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
However, invoking one of the methods on an unmatched element will raise a NoSuchElementError
.
Resources:
]]>1
|
|
But that was not working with paperclip version 4, a quick look at the stacktrace reveled the method I wanted to mock.
Failure/Error: report.save AWS::S3::Errors::InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records. /Users/me/.rvm/gems/ruby-2.1.0/gems/aws-sdk-1.33.0/lib/aws/core/client.rb:374:in `return_or_raise' /Users/me/.rvm/gems/ruby-2.1.0/gems/aws-sdk-1.33.0/lib/aws/core/client.rb:475:in `client_request' (eval):3:in `put_object' /Users/me/.rvm/gems/ruby-2.1.0/gems/aws-sdk-1.33.0/lib/aws/s3/s3_object.rb:1751:in `write_with_put_object' /Users/me/.rvm/gems/ruby-2.1.0/gems/aws-sdk-1.33.0/lib/aws/s3/s3_object.rb:607:in `write' /Users/me/.rvm/gems/ruby-2.1.0/gems/paperclip-4.0.0/lib/paperclip/storage/s3.rb:337:in `block in flush_writes' /Users/me/.rvm/gems/ruby-2.1.0/gems/paperclip-4.0.0/lib/paperclip/storage/s3.rb:314:in `each' /Users/me/.rvm/gems/ruby-2.1.0/gems/paperclip-4.0.0/lib/paperclip/storage/s3.rb:314:in `flush_writes' /Users/me/.rvm/gems/ruby-2.1.0/gems/paperclip-4.0.0/lib/paperclip/attachment.rb:239:in `save' /Users/me/.rvm/gems/ruby-2.1.0/gems/paperclip-4.0.0/lib/paperclip/has_attached_file.rb:87:in `block in add_active_record_callbacks' /Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:460:in `_run__2863894366922569793__save__4567171711544756900__callbacks' /Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:405:in `__run_callback' /Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-3.2.16/lib/active_support/callbacks.rb:385:in `_run_save_callbacks' # ...
after double checking the code on github, a working stub:
1
|
|
1 2 3 4 5 6 7 |
|
I quickly ran into the following exception:
Magick::ImageMagickError: WriteBlob Failed `/Users/ME/projects/CLIENT/code/RAILS_APP/tmp/line.png' @ error/png.c/MagickPNGErrorHandler/1804 from /Users/ME/.rvm/gems/ruby-2.1.0/gems/gruff-0.5.1/lib/gruff/base.rb:425:in `write' from /Users/ME/.rvm/gems/ruby-2.1.0/gems/gruff-0.5.1/lib/gruff/base.rb:425:in `write' from /Users/ME/projects/CLIENT/code/RAILS_APP/app/models/reports/chart.rb:36:in `generate_line_graph' from (irb):16 from /Users/ME/.rvm/gems/ruby-2.1.0/gems/railties-3.2.16/lib/rails/commands/console.rb:47:in `start' from /Users/ME/.rvm/gems/ruby-2.1.0/gems/railties-3.2.16/lib/rails/commands/console.rb:8:in `start' from /Users/ME/.rvm/gems/ruby-2.1.0/gems/railties-3.2.16/lib/rails/commands.rb:41:in `' from script/rails:6:in `require' from script/rails:6:in `main'
The issue: the error is raised when writing to the tmp directory, changing to another directory and it worked as expected?
g.write("public/line.png")
and g.write("public/tmp/line.png")
both worked
Turns out this was a simple mistake on my part, I just provisioned a new machine and cloned the project repo – I did not have a tmp directory in my project yet, it would be nice if the error message could have just told me so.
]]>Is it ‘time zone’, ‘timezone’ or ‘time-zone’? Both styles end up being used in rails code base
config.time_zone
andconfig.active_record.default_timezone
. I use timezone in this article and time_zone in the sample code base. Read more on stackexchange.
Sample code can be found at: https://github.com/house9/sample_timez
The sample includes user profiles with timezone settings, an event model, and a work schedule model. It comes with a small set of sample data as well. To set it up locally, follow the README instructions.
Rails uses the following defaults for a new application
I recommend you keep these defaults!
1 2 3 4 5 |
|
Both can be overriden in your application configuration file (config/application.rb) – don’t do it!
1 2 3 |
|
How you determine the ‘current users’ timezone will differ from application to application. In the sample code we are storing the value on the user model in a time_zone field. The sample application uses the techniques from this RailsCast episode. Alternatively you may want to set it based on the client’s browser setting.
Use an around_filter
or combo of before_filter
and after_filter
to set each request’s timezone in the ApplicationController
. If we don’t have a current user it will default to UTC.
1 2 3 4 5 6 7 8 9 10 |
|
The I18n
helpers are timezone aware. Aside from rendering the datetimes in the logged on users locale format, they will convert the stored UTC times to the current threads timezone.
On the homepage of the sample application there are numerous examples, also check out the config/locales/en.yml
file.
Basic example:
1
|
|
Beware: I18n.localize does not handle nil values. You will need to guard against nils for nullable columns.
In the sample application we have an Event model and a WorkSchedule model. Each model has its own timezone. Events are based on where the event will actually occur. In this case displaying the dates in the users timezone makes no sense at all, so we must override the display. There are a few techniques that can be employed:
in_time_zone
example: WorkSchedule overrides the start_at and end_at model attributes, therefore no special handling is needed in the views (of course you still need to use I18n.localize).
1 2 3 4 5 6 7 8 9 |
|
Time.use_zone
example: The Event model does not override start_at or end_at attributes, but uses Time.use_zone blocks in the views.
<!-- displayed times in the events timezone -->
<% Time.use_zone(@event.time_zone) do %>
<%= l(@event.start_at) %>
<%= l(@event.end_at) %>
<% end %>
<!-- now back to the logged on users timezone -->
<p>
Created at <%= l(@event.created_at) %>
Updated at <%= l(@event.updated_at) %>
</p>
There is odd behavior with Time.use_zone when doing in-memory sorting in ruby (sort_by
). See the HomeController
in the sample application. Ordered attributes do not convert in the use_zone block. I am not sure if this a bug or by design.
Sometimes you want to override saving data. Let’s say my profile is set up with ‘Pacific’ timezone, but I am creating an event that will occur in New York (‘Eastern’ timezone). I do not want rails to convert the times to ‘Pacific’. We can reset the current threads timezone to the events timezone before the create and update actions execute.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Alternatively this could be done using Time.use_zone
blocks in the create and update actions.
Rails will handle most of this automatically if you don’t use raw sql. For example:
1
|
|
If you are filtering datetime data by the day be careful, the below queries are from the sample application logged in as a user with (GMT+06:00) Astana
timezone
1 2 3 4 5 6 |
|
the proper way
1 2 3 4 5 6 |
|
This is not an issue when filtering on date columns (only datetime).
1 2 3 4 |
|
In some situations you might need to query for data based on a specific timezone
1 2 3 4 5 6 7 8 |
|
I hope you found this article helpful. All feedback is welcome – I am sure I have left out some key information or just plain got it wrong.
Additional Reading: http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails
1
|
|
1 2 |
|
1 2 3 4 |
|
I was first diagnosed with IBS in 2002 and was told to
I had never heard of such a thing as ‘IBS’ before, it seems it is the ‘catch-all’ diagnosis for stomach issues when the doctors do not know for certain what is causing the issue.
Things that did help me, but I would not say they ‘cured’ me:
Even after changing my diet I still had discomfort off and on. At one point in 2007 I thought my appendix was going to burst, went to the emergency room to find out my appendix was fine and that I was just really constipated. Talk about embarrassing.
After that I decided to try some acupuncture treatment. It did help some, but the thing that ‘cured’ my IBS was the advice my acupuncturist gave me:
The jist of it is when its time to eat, its time to eat and nothing else:
Doing the above, my IBS has dwindled away, it did not happen overnight, it took many months of being very strict about my eating habits – mainly taking the time to digest after a meal before engaging in any work activities.
I am somewhat of a grazer, I like to eat small amounts through out the day while working. My work consists of being on the computer over 90% of my work day. Sometimes my work is stressful, even when it is not ‘stressful’ it is still very engaging and involves a very high level of concentration.
I believe I was training my body to think it was under stress whenever I was eating, weather I was on the computer or not.
If you suffer from IBS I hope the above will work for you as well.
-Namaste
]]>Followed the fine documenation here: http://octopress.org/docs/ to get up and running.
And used this script https://gist.github.com/juniorz/1564581 to import all of my old posts from blogger. It failed to import a few posts but overall worked great!
I do have various formatting issues on older posts, will need to revisit those in the future…
Authoring with markdown is so much better and no need for embedded gist: ya!
1
|
|
1
|
|
See the github repo at https://github.com/house9/puppet-solo-hello-world
Do you have a recovery plan in case your Postgres server crashes – your daily pg_dump is probably not going to cut it.
Postgres uses Write-Ahead Logging (WAL)
Write-Ahead Logging (WAL) is a standard method for ensuring data integrity. A detailed description can be found in most (if not all) books about transaction processing. Briefly, WAL’s central concept is that changes to data files (where tables and indexes reside) must be written only after those changes have been logged, that is, after log records describing the changes have been flushed to permanent storage. If we follow this procedure, we do not need to flush data pages to disk on every transaction commit, because we know that in the event of a crash we will be able to recover the database using the log: any changes that have not been applied to the data pages can be redone from the log records. (This is roll-forward recovery, also known as REDO.)
Setting up Continuous Archiving and Point-in-Time Recovery (PITR) for your Postgres WAL files is very complex, lucky for us the WAL-e project has simplified this process greatly. WAL-e has utilities for sending all WAL files to an AWS S3 bucket as the log files are being generated. More Information, see:
Recently I put together a chef cookbook which installs WAL-e on a Postgres instance, see:
]]>