Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Sunday, 13 February 2022

Upgrading to Rails 7

I run some simple Rails applications which I keep upgraded. I recently upgraded Ruby and Rails versions and since it's upgrade season, I'm making a note of my experiences ready for the next application I do, and on the off-chance it can help other people get started. And a reminder that I do do technical things occasionally, honest.

To note - this is a very simple Rails application, so only covers the basic gotchas I experienced. It was an app originally written in Ruby 2 / Rails 5 and in this iteration is now being upgraded from Ruby 2.7 / Rails 6 to Ruby 3 / Rails 7. The code is stored in a repository in Github, with CI done both via Github Actions and Codeship and deployment to Heroku.

Ruby upgrade

Good news! The Ruby upgrade (2.7.2 to 3.0.3) caused no problems at all, although I did need to update my Codeship config to remove the explicit installation of Bundler in my setup script.

Rails upgrade

I made use of the Rails upgrade guide - in particular I bounced the version of Rails in my Gemfile from 6 to 7 then ran bundle exec rails app:update which introduced the usual ton of rubocop violations but also created some migrations acting on some tables which didn't actually exist.

These were ActiveStorage tables so I needed to create them with bundle exec rails active_storage:install then rearrange the migrations to get them to work (ie put the creation migration before the modify migrations). I probably could have eliminated the new migrations since I'm not using this function, but it seems there are other references to these tables. It looks like Rails expects these tables to exist, and the lazy option was to create it and be on the standard path. So I was lazy. Those migration do not include timestamps, so I had to add those to appease the linter.

Second show-stopping problem - Rails 7.0.2.1 was a trap. It turns out there was a bug in ActiveSupport which manifested in a variety of different ways. For me, it stopped almost all automated tests working. It was reported and fixed quickly but did manage to cost me a chunk of time trying to figure out what was going on... Anyway, the simple fix is to use Rails 7.0.2.2 instead.

Now fixing some deprecation warnings. action_cable has been renamed to actioncable and this will escalate to a breaking change in Rails 8 so this needs updating in app/assets/javascripts/cable.js.

Also, I was getting references to using older (Rails 5.1) CSRF protection behaviour. There are a load of defaults which are version locked in config/application.rb and these can be upgraded with config.load_defaults 7.0 as per the upgrade guide.

Locally, this was all that was required however when I deployed to production, the heroku build failed at bundle exec rake assets:precompile. Uglifier needed ES6 syntax enabling, with config.assets.js_compressor = Uglifier.new(harmony: true) in config/environments/production.rb.

And that was it! As I said, a very simple application so I'm sure I avoided much of the upgrade pain but I've noted my experiences here to help others get started.

All these changes are captured in a pair of pull requests:

Saturday, 29 December 2018

Mental health and maths

There are a million connections between social media and mental health and most I know about show a strong (albeit anecdotal) negative connection between the two. People are constantly saying they've taken a break from Twitter and feel much better for it, and I know that for me avoiding "debating" (or more accurately "screaming impotently into the void") on social media is definitely a good thing.

But what happens if we look at this backwards? Engaging with the world, as opposed to withdrawing and shutting down is supposed to be positive, right? In theory, social media is about reaching out and connecting (haha I know, but stay with me). I know that when I'm feeling happy and on top of the world I look at it with interest and very often take photographs of the things I see. Some of those photographs end up on Instagram. When I'm exhausted and my mental health is poor I shut down to preserve energy and I feel like I'm doing a lot less, such as taking fewer photographs.

So in theory I should be able to see a correlation between taking lots of photos and posting them to Instagram and times of good mental health. Good - I've needed an excuse to write some code. Cue the music.

A bit of hacking later and I've got a bit of Ruby which looks at my Dropbox Camera Uploads directory and counts the photos I've taken this year, grouping by month. And I've got another bit of code which looks at my Instagram feed and does the same, but using a deprecated API because I'm not a business and apparently that means I'm out of luck in their new world.

And lo:
The very obvious peak in October is at a time when I had a week off, had just had some very good news and actually Did Things outside of work (walked the Two Tunnels walk in Bath and went to a display of Japanese art in Bristol). Coincidentally I also wrote a post about having a peaceful time off. So it was a good month - or at least a good week in a month.

At the other end of fun, the two obvious troughs are in July and November. This matches my memory of the year - times when I had to drag myself out of a major slump. This was especially true in November, when I had far too much to do both in and out of work. In work a long series of things went wrong and I was on the back foot the entire time - which resulted in me writing a post about reactive vs proactive work. Outside of work I basically stopped doing anything as I worked late and spent the evenings recovering. I felt my engagement with the world around me suffered and this is born out in the numbers.

Otherwise it's pretty stable through the year other than an upwards tick in August (another holiday), so what happens if we add a trend line?
An increase in photos taken over the course of the year, but interestingly a decrease in photos posted to Instagram. So I'm snapping more, but gradually getting less interested in the world around me? That's quite possible. I mean, have you seen the world?

Last year I had an interesting time and I started this year writing about recovering from an actual crash. So what happens if we overlay 2017 Instagram engagement with 2018 engagement?

*hack hack*

This happens:
I was at my lowest in November and early December 2017, which correlates nicely with ... the highest engagement of the year. Hmm - that messes with my theory. December I can easily explain - I was away from work for most of that month, actively recovering, and spent Christmas with my parents walking miles and taking photos of their village. November is a mystery. Looking back, it seems in November I went for a few long walks around London and posted multiple pictures from each which brought the count up. Confusing. Moving on to trends.
Rate of posting to Instagram increased over 2017, which is mostly expected with the exceptions above. The decline in 2018 still worries me, as does the change in style of my pictures over the last 24 months. I used to put more thought into use of abstract space and filters, giving more texture to the posts. More recent photos are pictures with limited work on them - pictures of things rather than more artistic expressions, if you'll forgive the phrasing. It's not all bad - the technical quality of my pictures has definitely improved - however it plays to the narrative of less engagement from me in the output.

So what have I learned? This kinda works. It's certainly not good enough to create an early-warning system to tell me to Take Steps if my frequency of posting drops below a certain level. But there are some interesting patterns and going through this has got me thinking about changes I can make in life which will hopefully make a positive impact. I have strong, positive memories of the various Things I've done, and those Things tend to generate photographs. So if, loosely, I can do more Things I should improve my general outlook on life. I hope? I spend a lot of time in London, so finding something interesting and photo-worthy to do once a month shouldn't be the hardest thing in the world. Now I need an arbitrary point in the year at which to make fresh-start promises to myself. Some kind of Resolution for a New Year, perhaps?

If you're interested in details, the code is all on Github and  I used Google Sheets for the spreadsheet to generate the graphs. I am not a doctor and I'm well aware this is not Science.

Saturday, 26 October 2013

Jekyll and Foundation with Guard

tl;dr - I've dropped the jekyll asset pipeline in favour of Guard in my pre-configured example project of Jekyll and Foundation.

A few weeks I wrote about my attempts to create a bootstrapped jekyll / foundation combination using the Jekyll asset pipeline to keep things building on the fly. I hit a snag, though. The asset pipeline was abandoned back in February and is locked to version 0.12 of Jekyll (current release being 1.2.1 at the time of writing). There are various forks of the project which bring things up to date but I'm worried about being left in the same position again and, frankly, I'm starting to question the methodology of using Jekyll to do everything instead of running the tools separately.

What I really want, I think, is some kind of meta-tool which lets me manage my build environment but by using the core tools - in my case Jekyll and Compass and probably a CoffeeScript processor eventually - so I (hopefully) have fewer maintenance problems down the line. Seems like Guard is pretty much what I want. I'm coupling it with guard-jekyll-plus and guard-compass to work with those tools. It isn't perfect, and doesn't assuage my dependency fears entirely but it feels a much more robust solution than the previous version.

My basic jekyll-foundation project has been changed to use this new tool chain and seems much more responsive than before (could be anecdotal) so I'm looking forward to making more use of it.


Monday, 30 September 2013

Jekyll and Foundation automatically recompiling changes

tl;dr - I've glued jekyll, jekyll pipeline and foundation into a pre-configured example project.

I look after a handful of small sites and I've been playing with different ways of maintaining them and keeping them vaguely modern. A CMS is the obvious way forward, but that has the learning curve problem and, frankly, after working away at this kind of thing all day I'd rather approach the problem a little differently.

What I really want is something which lets me write content in my markup language of choice, then generates some html for me and wraps it in a template. It should also handle some of the simpler optimization tasks (minifying, compressing, compiling any sass) and ideally allow some kind of on-the-fly regeneration of all these assets so I can work and hit refresh with it running in the background.

Jekyll seems like a good contender as a starting point. I love the fact it generates static HTML files for me (I'm getting tired of requiring the comparatively slow processing power of PHP for ultra-simple tasks) and it has a "watch" mode for recompiling the pages as I work. It uses the Liquid templating engine, which is sufficiently simple for my needs, and there is support for a variety of markup options.

Next, I need some sass support. This is easily done, but I want a simple workflow so it needs to recompile on the fly while jekyll watch is running. Step up the jekyll asset pipeline which lets me add whatever I like to the jekyll process with a minimum of fuss, including css compression and the sass compilation I wanted. I'm pretty sure I could add CoffeeScript support with it too, but I haven't tried that yet.

Right, I have the components I need. I'm going to work with Foundation 4 because it happens to be a framework I know relatively well. If only there was a vanilla project which pulled all this together ready so I could just fork it and start building a new site. Oh look.

In all seriousness, I've hammered these elements together into something that should be easy to just pick up and start using. I'm hoping it'll be useful to me and - this being the internet - chances are that means it'll be useful to someone else too. If that someone is you, enjoy this vanilla jekyll project with foundation sass built on the fly. Why not let me know?

Wednesday, 26 March 2008

rubygems

Rubygems on Ubuntu seems to break itself when you update:

sudo gem update --system

I've lost track of the number of times I've had to fix this (and inevitably forgotten how to do it each time) so for anyone interested here is the fix:

* Open a terminal
* sudo vi `which gem`
* under the line:
require 'rubygems'
add:
require 'rubygems/gem_runner'

Job done.

I can't remember where I first saw this fix, but I know I've found it here at least once, so thanks for the help Nick.