I'm not angry but...

I spent much of yesterday trying to Paperclip working with some S3 extensions that I’ve used in countless projects and it just didn’t work. If you’re interested, well, here’s a slice of it.

module Paperclip
  module Storage
    S3.prepend(Module.new do
      def url(style_name = default_style, options = {})
        with_environment { super }
      end

      def expiring_url(time = 3600, style_name = default_style)
        with_environment { super }
      end
    end)
  end

  module Storage
    module S3
      def download_url(time = 3600, style_name = default_style)
        with_content_disposition("attachment; filename=#{instance_read(:file_name).without_extended_characters}") do
          expiring_url(time, style_name)
        end
      end

      def inline_url(time = 3600, style_name = default_style)
        with_content_disposition('inline') do
          expiring_url(time, style_name)
        end
      end

      private def with_content_disposition(disposition)
        @options[:s3_url_options] ||= {}
        @options[:s3_url_options][:response_content_disposition] = "attachment; filename=#{instance_read(:file_name).without_extended_characters}"
        yield
      ensure
        @options[:s3_url_options].delete(:response_content_disposition)
        @options.delete(:s3_url_options) if @options[:s3_url_options].empty?
      end

      # overrides s3 credentials to serve production assets when in
      # staging or development
      private def with_environment
        # only override if dev or staging
        return yield unless Rails.env.development? || Rails.env.staging?
        # only override if updated in the last day
        return yield if (instance_read(:updated_at) || 2.days.ago) > 1.day.ago

        begin
          @s3_interface = nil
          @bucket = nil
          @s3_bucket = nil
          @s3_credentials = parse_credentials(Settings.get(:s3, 'production-readonly')[:s3_credentials])

          yield
        ensure
          @s3_interface = nil
          @bucket = nil
          @s3_bucket = nil
          @s3_credentials = parse_credentials(Settings.get(:s3)[:s3_credentials])
        end
      end
    end
  end
end

With Settings to extract settings and cope with overriding different environments. Say, to display production images in development using the above. I’ve found it very useful.

module Settings
  def self.get(name, environment = Rails.env.to_s)
    fetch("#{name}.yml")[environment]
  end

  def self.fetch(config)
    yaml = paths(config).inject([]) do |yamls, path|
      yamls << IO.read(path)
    rescue Errno::ENOENT
      yamls
    end.reject(&:blank?).join("\n")

    if Rails.env.production? && yaml.include?('&defaults') && !yaml.include?("production:\n")
      yaml += "\nproduction:\n  <<: *defaults"
    end

    YAML.load(ERB.new(yaml).result)
  end

  def self.paths(config)
    [
      Rails.root.join('config', 'settings', config), # repo
      File.join(ENV['HOME'], application, 'shared', 'settings', config), # server
      File.join(ENV['HOME'], '.settings', application, config) # local
    ]
  end

  def self.application
    Rails.application.class.name.split('::')[0].underscore
  end
end

Except for yesterday when I forgot or didn’t realise that the storage option in Paperclip needs to be a symbol. If it’s a string it gets ignored.

def self.get(name, environment = Rails.env.to_s)
    fetch("#{name}.yml")[environment].deep_symbolize_keys # <---- hours wasted
  end

And finally the methods that extend Attachment::Storage::S3 don’t appear because it defaults to :filesystem unless you supply :s3 as a symbol. Ok, finally I am there… No.

AWS::S3::Errors::AccessDenied Access Denied

But I’ve added the Bucket Policy to use my IAM user, and set the correct permissions. I know this because I compared it to another policy I set up a week prior.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::1234567890:user/username"
            },
            "Action": [
                "s3:ListBucket",
                "s3:GetBucketPolicy",
                "s3:PutBucketPolicy"
            ],
            "Resource": "arn:aws:s3:::bucketname"
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::1234567890:user/username"
            },
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::bucketname/*"
        }
    ]
}

Files didn’t upload at all. S3 credentials are correct. But still the same error. As then as it turns out, S3 buckets can be on this occasion was set to Bucket and Objects not public. So eventually I realise that the Paperclip upload default is :public and if I am trying to do that then I get AWS::S3::Errors::AccessDenied Access Denied sigh

defaults: &defaults
  storage: :s3
  bucket: bucket
  s3_protocol: :https
  s3_permissions: :private
  s3_credentials:
    access_key_id: accesskey
    secret_access_key: secretaccesskey
    s3_region: us-east-1

development:
  <<: *defaults

production:
  <<: *defaults
  bucket: bucket

So finally…at 2am. This is where we are. I enjoy the chase, I also enjoy sleep.

We need a name for our new board game

We painstakingly put together this prototype in just under 10 mins.

Ruby 1.8.7 and Rails 1.2

We have recently been looking at hiring so I created a stupid question just to get a developer talking. When I first started working with Rails we were building Euro-reg.com with Ruby 1.8.6 and Rails 1.2.3 as far as I can recall. So the question ended up being…

Suppose we hired you and on day one said we have this Rails 1.2 Application on Ruby 1.8.7, using SQLite in production that is mission critical. How would you approach bringing it up to date?

Some feedback we got was “…Rails v1.0—2.1 are incompatible with Ruby v1.8.7…” which made me question my stupid question. Actually it’s been so long I can’t even remember. So 10 minutes later.

The flow was

That was nicely quick, I guess it could have been horrible.

Want to find files with exactly the right size

Have you ever come across some files in your application that just appear to be generated by err…..a generator and actually do nothing. Well I find a couple of javascript files with the same content (and therefore the same size) and wondered if there were any more.

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

Well fortunately you can use find with a specific option

Robs-MBP:blah rl$ find app/assets/ -size 229c
app/assets//javascripts/some.js.coffee

Excellent, I can ditch that file too then.

HAML whitespace

I came across a weird HAML syntax in a client codebase that I was just confused about.

Haml::Engine.new(%q{
- title = '1'
.title== #{title}<br/>
}).to_html
=> <div class='title'>1<br/></div>

I found an alternative syntax, I am not sure I like it but it’s certainly better than the above.

Haml::Engine.new(%q{
- title = '1'
.title<
 #{title}
 %br>
}).to_html
=> <div class='title'>1<br></div>

Basically you can kill whitespace with < >

Bundle deprecations

If you want to search your bundle for anything in particular, in this case the deprecated Digest::Digest syntax

New-MacBook-Pro:that cex$ bundle show --paths | xargs grep -r Digest::Digest
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/aws-sdk-1.6.4/lib/aws/core/signer.rb:        OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(digest), key, value)
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:      @digest = OpenSSL::Digest::Digest.new('sha1')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:        @digest = OpenSSL::Digest::Digest.new('sha256')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/cloudstack.rb:    @@digest  = OpenSSL::Digest::Digest.new('sha1')
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/object_types matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/object_types matches

Everything you need in life

I’ve thought about this plenty and I’m more convinced than ever that the list of things you need in your life are

If there any suggestions on things to add to this list I will consider them but probably not too thoroughly.

When Postgresql doesnt start on OSX

New-MacBook-Pro:mini-epic cex$ ./bin/server 
=> Booting Puma
=> Rails 4.2.1 application starting in development on http://0.0.0.0:8001
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/cex/.rvm/gems/ruby-2.2.2@epic-invite/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I tried restarting with brew

New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Same deal. Thanks to https://stackoverflow.com/questions/13410686/postgres-could-not-connect-to-server Remove the PID and you’re sorted.

-rw-------  1 cex  admin  88  2 Mar 15:02 /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ rm /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Sometimes....

Sometimes it gets bloody…

AASM kick in the balls bug / typo / misunderstanding

Pretty average AASM block right? Wrong….

I was just dealing with a bug where object.mark_sent! wasn’t saving the record.

included do
  include AASM
  aasm(:state) do
    state :in_checkout, initial: true
    state :processing
    state :available
    state :completed
    state :inactive
    state :sent

    event :start_processing! do
      transitions from: [:in_checkout, :processing, :available], to: :processing
    end

    event :processed! do
      transitions from: [:in_checkout, :processing], to: :available
    end

    event :mark_sent! do
      transitions from: [:available, :completed], to: :sent
    end

    after_all_transitions :set_aasm_timestamp
  end
end

There’s a mark_sent! event, so surely mark_sent! should change the state and save the record right? No.

event :mark_sent! do
  transitions from: [:available, :completed], to: :sent
end

This actually creates two method mark_sent! which makes the state change and it’s counterpart that also saves the record mark_sent!!. You can’t call a method with a double exclamation directly.

[26] pry(main)> e.mark_sent!!
[26] pry(main)* 
[26] pry(main)* e.send(:'mark_sent!!')

When in Vegas, don’t define mark your events with an exclamation. DOH.