Having seen a few issues with cross domain web fonts with Firefox and a report of the same in Chrome. This small addition to an Nginx server config allows access from any origin, in this case for a fonts in a Rails asset pipeline. This config solves the problem of another site pinching our fonts. However, more importantly this allows cloudfront to sit in front of our origin server and for our site to serve fonts over cloudfront.
location ~ "^/assets/(.*/)*.*-[0-9a-f]{32}\.(woff|ttf|eot)$" {
gzip_static on;
expires 1h;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
# debug to check this is being caught.
add_header X-Braindeaf-Pipeline true;
}
With the right syntax, we should probably limit the origin to *.cloudfront.net domains, and to our specific distrubitions in a more complete solution.
Since we’re heading off to Germany in the morning, here’s a quick stab at the Google Maps Image APIs Static Maps . Looks fairly straight forward if you have the Lat/Long of the places you want to create markers for.
I needed to tweak my Nginx config to redirect / (slash) only on a Wordpress site to a completely different location but leave all other routing intact. There we have it.
class Jbuilder
# don't repeat yourself. when WillPaginate
def paginate!(collection, &block)
if collection.respond_to?(:page)
set! :total_entries, collection.total_entries
set! :page, collection.current_page
set! :per_page, collection.per_page
else
set! :total_entries, collection.count
set! :page, 1
set! :per_page, collection.count
end
set! :entries, collection, &block
end
# don't return null, should should be an empty string
def _set_value(key, value)
raise NullError, key if @attributes.nil?
unless @ignore_nil && value.nil?
if value.nil?
@attributes[@key_formatter.format(key)] = ''
else
@attributes[@key_formatter.format(key)] = value
end
end
end
# when you want to skip a record when iterating over a collection
def _map_collection(collection)
return [] if collection.nil?
collection.map do |element|
_scope{ yield element }
end.reject(&:blank?)
end
end
Ran into a problem whereby the counter cache on one of our associations was out of sync. Simple …just update the counter cache with the count of the association?
rails >> u User.find(1)
rails >> u.update_attribute(:roles_count, 1)
ActiveRecord::ActiveRecordError: roles_count is marked as readonly
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/persistence.rb:194:in `update_column'
from (irb):17
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start'
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start'
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>'
from /data/app/current/script/rails:6:in `require'
from /data/app/current/script/rails:6:in `<main>'
rails >> u.update_column(:roles_count, 1)
ActiveRecord::ActiveRecordError: roles_count is marked as readonly
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/persistence.rb:194:in `update_column'
from (irb):17
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start'
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start'
from /data/app/shared/bundled_gems/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>'
from /data/app/current/script/rails:6:in `require'
from /data/app/current/script/rails:6:in `<main>'
rails pro >> User.readonly_attributes
=> #<Set: {"roles_count"}>
Seems you can’t update the value, I was surprised that update_column wouldn’t work either. I guess it makes sense to protect this value since it’s supposed to be kept in sync and only in rare occasions should it be out of sync. However, you can use the reset_counters class method.
rails >> u = User.find(1)
rails pro >> User.reset_counters u.id, :roles
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
(0.4ms) SELECT COUNT(*) FROM `roles` INNER JOIN `role_users` ON `roles`.`id` = `role_users`.`role_id` WHERE `role_users`.`user_id` = 1
(0.9ms) UPDATE `users` SET `roles_count` = 1 WHERE `users`.`id` = 355411
=> true
Suppose I want to overwrite a method an apply a optional argument that the original method didn’t accept.
class Something < ActiveRecord::Base
def to_s(or_other = nil)
if or_other
Other.new.to_s
else
super
end
end
end
Seems fine. However, when you call to_s you’d get an error like wrong number of arguments (1 for 0). The reason is because without supply arguments to super they, by default, use the arguments passed to the overridden method. Since the original method didn’t accept any arguments previously then this fails. In order to avoid this you must call super() to deliberatly call it without arguments.
class Something < ActiveRecord::Base
def to_s(or_other = nil)
if or_other
Other.new.to_s
else
super()
end
end
end
Just wanted to test out OmniAuth by plugging into PledgeMusic authentication.
github. braindeaf/omniauth-pledgemusic
Well it works and you get an access token out, nice and simple. Wouldn’t mind re-styling the OmniAuth::Form to be a bit more in keeping with the decor.
After finding the GitHub jQuery Repo Widget I want to embed the widget in my blog posts. The body of which uses RedCloth So I needed to add a custom tag which could turn.
You can extend RedCloth’s HTML formatter by placing something like this in config/initializer/redcloth_extensions.rb for example.
require 'redcloth'
module RedCloth
module Extensions
def github(options)
html = %Q{<div class="github-widget" data-repo="#{options[:text]}"></div>\n}
end
end
end
RedCloth::Formatters::HTML.send(:include, RedCloth::Extensions)
In a recent project I wanted to use a date to be a component of a hashed string in order to create a unique hash for a password reset page. I want the hash to expire when the user’s password is reset, in this case when the user’s last_logged_in datetime changes. I can generate the hash simply.
class User < ActiveRecord::Base
....
def password_token
unless new_record?
Digest::SHA1.hexdigest([id, password_hash, salt, last_logged_in.to_i].join)
end
end
....
end
I don’t want to store this hash in the database (we might want to in the future to optimise the look up). So I need to generate the hash in SQL, but how to I do the equivalent to Time#to_i in SQL.
class User < ActiveRecord::Base
....
class << self
....
def with_password_token(password_token)
where("SHA1(CONCAT(users.id, IF(users.password_hash IS NOT NULL, users.password_hash,''), IF(users.salt IS NOT NULL, users.salt, ''), IF(users.last_logged_in, UNIX_TIMESTAMP(users.last_logged_in), ''))) = ?", password_token).first
end
....
end
....
end