Post # 242

Oh Dear

Completed 200 OK in 4100900.7ms (Views: 3288.4ms | ActiveRecord: 4097424.5ms)

AngularJS and Uglifier don't mix well.

Uglifier’s mangling messes with AngularJS. To turn it off you should do something like…

# config.assets.js_compressor = :uglifier
config.assets.js_compressor = Uglifier.new(mangle: false) if defined? Uglifier

Checkout a single file from a different branch

I totally managed to mess up my Gemfile.lock file setting the versions of many gems to far higher versions than I’d intended. bundle update is bad, even if you set the version in your Gemfile the dependencies of thos gems are not versioned so precisely. I did this in a branch so fortunately the Gemfile.lock in my master branch is accurate.

git checkout master -- Gemfile.lock

That did the job. Then a bundle install puts the changes I’d made to my Gemfile into Gemfile.lock with the correct dependencies as per master. Phew.

Brew fails after installing Yosemite

If you upgrade to MacOSX Yosemite and get this

Robs-iMac:~ rl$ brew install

/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory

/usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0

you need to do this

cd /usr/local/Library

git pull origin master

Then you’re good again.

Xcode Licence blocking gem install

Hmmmzzz..

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/rl/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb 
creating Makefile

make "DESTDIR=" clean


Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.



make "DESTDIR="


Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.



make failed, exit code 69

Gem files will remain installed in /Users/rl/.rvm/gems/ruby-2.1.2@upgrade-ruby/gems/byebug-3.5.1 for inspection.
Results logged to /Users/rl/.rvm/gems/ruby-2.1.2@upgrade-ruby/extensions/x86_64-darwin-13/2.1.0-static/byebug-3.5.1/gem_make.out
An error occurred while installing byebug (3.5.1), and Bundler cannot continue.
Make sure that `gem install byebug -v '3.5.1'` succeeds before bundling.

You need to do sudo xcrun cc

..blah blah blah...

By typing 'agree' you are agreeing to the terms of the software license agreements. Type 'print' to print them or anything else to cancel, [agree, print, cancel] agree

JSONP response in Rails Controller

If you’re used to rendering your JSON response via call to_json on a collection on an object then it really is as simple as adding the callback option to render. You must ensure the callback parameter on the client end is the correct parameter than you feed to the callback option in your controller. This allows your JSON to be server without or without wrapping the response with a callback function depending on whether you include the callback parameter.

class Api
  
  def index
    respond_to do |format|
        format.json do
          render :json => Thing.all.to_json, :callback => params[:callback]
        end
    end
  end

end

However, if you use JBuilder or any other JSON template engine then you’ll need to do things slightly differently. Here There is no need to change your controller method at but instead you can wrap the json repsonse in an after_filter which is a little cleaner.

class Api < ApplicationController

  after_filter :wrap_response_with_callback

  def index
    respond_to do |format|
        format.json
    end
  end

  private

  def wrap_response_with_callback
    if request.get? && params[:callback] && params[:format].to_s == 'json'
      response['Content-Type'] = 'application/javascript'
      response.body = "%s(%s)" % [params[:callback], response.body]
    end
  end

end

extending a class included in a module

This seemed pretty straight forward to me.

module Something
  def api
   @api ||= Api.new
  end
  class Api
    def do_something
      :doing_something
    end
  end
end

class Thing
  include Something
  class Api
    def do_thing
      :doing_thing
    end
  end
end

Seems legitimate, just extend the class that I’ve included in my module.

2.1.2 :021 > Thing.new.api.do_thing
NoMethodError: undefined method `do_thing' for #<Something::Api:0x007f946546afe0>
	from (irb):21
	from /Users/rl/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'

Wrong! Hmmzz…. I guess the class that the module is instantiating from is the one in the module not the one included in my Thing class I need to ensure that I instantiate from the right one.

module Something
  def api
   @api ||= self.class::Api.new
  end
  class Api
    def do_something
      :doing_something
    end
  end
end

class Thing
  include Something
  class Api
    def do_thing
      :doing_thing
    end
  end
end
</pre></code>

Works, but hang on.

<pre><code>2.1.2 :019 > Thing.new.api.do_thing
 => :doing_thing 
2.1.2 :020 > Thing.new.api.do_something
NoMethodError: undefined method `do_something' for #<Thing::Api:0x007fa60a4c1fc8>
	from (irb):20
	from /Users/rl/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'

The class is not using the class I’ve defined in my module at all.

module Something
  def api
   @api ||= self.class::Api.new
  end
  class Api
    def do_something
      :doing_something
    end
  end
end

class Thing
  include Something
  class Api < Something::Api
    def do_thing
      :doing_thing
    end
  end
end

Better,

Imagemagick creating a blank PNG

You can create a transparent PNG with Imagemagick fairly easily.

Robs-MacBook-Pro:lcf roblacey$ convert -size 200x100 xc:none transparent.png

Suppose you wanted to base64 the output to embed it in HTML

Robs-MacBook-Pro:lcf roblacey$ convert -size 200x100 xc:none png:fd:1 | base64 -i - -o -
iVBORw0KGgoAAAANSUhEUgAAAMgAAABkAQAAAADr/UKmAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAnRSTlMAAHaTzTgAAAACYktHRAAB3YoTpAAAABlJREFUSMftwYEAAAAAw6D5Ux/gClUBAMAbCigAAUr66mAAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTQtMTEtMDdUMDI6MDE6NDcrMDA6MDCn12GlAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE0LTExLTA3VDAyOjAxOjQ3KzAwOjAw1orZGQAAAABJRU5ErkJggg==

Of course when I do this in HTML…..

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkAQAAAADr/UKmAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAnRSTlMAAHaTzTgAAAACYktHRAAB3YoTpAAAABlJREFUSMftwYEAAAAAw6D5Ux/gClUBAMAbCigAAUr66mAAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTQtMTEtMDdUMDI6MDE6NDcrMDA6MDCn12GlAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE0LTExLTA3VDAyOjAxOjQ3KzAwOjAw1orZGQAAAABJRU5ErkJggg==">

The image is transparent and therefore you can’t see it.

This one is mildly more interesting, maybe.

Robs-MacBook-Pro:lcf roblacey$ convert -size 200x100 xc:"#000000" png:fd:1 | base64 -i - -o -
iVBORw0KGgoAAAANSUhEUgAAAMgAAABkAQAAAADr/UKmAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAnRSTlMAAHaTzTgAAAACYktHRAAB3YoTpAAAABlJREFUSMftwYEAAAAAw6D5Ux/gClUBAMAbCigAAUr66mAAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTQtMTEtMDdUMDI6MDE6NDcrMDA6MDCn12GlAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE0LTExLTA3VDAyOjAxOjQ3KzAwOjAw1orZGQAAAABJRU5ErkJggg==

Wait for it…….

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkAQAAAADr/UKmAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAd2KE6QAAAAZSURBVEjH7cGBAAAAAMOg+VMf4ApVAQDAGwooAAFK+upgAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE0LTExLTA3VDAyOjEwOjM1KzAwOjAwFMHNawAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNC0xMS0wN1QwMjoxMDozNSswMDowMGWcddcAAAAASUVORK5CYII=">

…worth waiting for.

Today is brought to you by....

TextileEditorHelper even though I had to then hack the helper to not overwrite the textarea class.

module ActionView::Helpers

  module FormHelper

    def textile_editor(object_name, method, options = {})
      output = []
      name = "#{object_name}[#{method}]"
      output << text_area_tag(name, nil, options.merge(class: [options[:class], 'textile_editor'].reject(&:blank?).join(' ')))
      output.join("\n").html_safe
    end
  
  end

end

Trying to work out why compressing jQuery in Rails 4 breaks IE8. Then realising.

jQuery 2.x has the same API as jQuery 1.x, but does not support Internet Explorer 6, 7, or 8.

and the BitMapCatNap