llptips
llptips
Lunar Logic Tips
32 posts
Random web & mobile development tips from the Lunar Logic team
Don't wanna be here? Send us removal request.
llptips · 13 years ago
Text
How to speed up assets:precompile
The `rake assets:precompile` task is used in Rails 3.1+ to generate asset files in production. However, if you look in the `public/assets` directory, you'll see that for every asset it generates both `file-.js` and `file.js` (and their `.gz` versions). The non-digest file is meant as a fallback for the cases where someone wants to refer to the file without its hash - but this is not recommended anyway, because these files will be cached by the browser and then might not be refreshed when you put a new version on the server under the same name. If you're sure that you're using asset path helpers everywhere and you don't need the non-digest files, use this task instead: RAILS_ENV=production rake assets:precompile:primary This will only generate the digest versions of assets and will take 50% less time. The `RAILS_ENV` is necessary here - `assets:precompile` adds it automatically, but `assets:precompile:all` doesn't, so if you don't add it, it will run in development mode and will only generate non-digest files. Check out also [this article](http://psionides.eu/2012/05/06/extending-asset-pipeline-with-custom-preprocessors/) to see how to make it easier to generate asset paths in your Javascript files.
21 notes · View notes
llptips · 13 years ago
Text
How to write specs for ApplicationController
Everyone knows how to test normal controllers in RSpec - you just call e.g. `get :index` and check the results. But the ApplicationController is different, since it doesn't contain actions, but rather filters and helpers, which are usually private methods. Most people's initial reaction is probably to just give up and don't test it. However, newer versions of RSpec have a simple DSL that makes it very easy to test code in ApplicationController. The trick is to use `controller { ... }` block to define an anonymous subclass on the fly and then test it like a normal controller. For example, if you want to test such filter:
class ApplicationController before_filter :check_browser def check_browser if request.env["HTTP_USER_AGENT"] =~ /MSIE/ redirect_to 'https://www.google.com/chrome/' end end end
You just need to create a controller that has one action, make sure that it uses the filter (if it's not included by default), and call that action:
describe ApplicationController controller do def index end end context "if user uses IE" do before { request.env["HTTP_USER_AGENT"] = "MSIE 6.0" } it "should redirect to Chrome website" do get :index response.should redirect_to('https://www.google.com/chrome/') end end end
You can create different `controller {}` blocks for any `describe` or `context` block, so that each of them only calls the helper/filter that you want to test in the block.
4 notes · View notes
llptips · 13 years ago
Text
How to tell an SQL query to use a specific index
If you have a case where you have several indexes on one MySQL table and for some reason the database isn't choosing the most optimal one for a given query, you can pass it a list of indexes that can be used in the query:
SELECT * FROM users USE INDEX (users_name, users_city)
Now MySQL will only attempt to use the users_name or users_city indexes if it's possible - if there are any other indexes, they will be ignored.
You can also use IGNORE INDEX which does the opposite:
SELECT * FROM users IGNORE INDEX (users_age)
24 notes · View notes
llptips · 13 years ago
Text
How to get content of confirm or alert window using webdriver
page.driver.browser.switch_to.alert.text
You can also invoke "accept" and "dismiss" methods on alert object.
5 notes · View notes
llptips · 13 years ago
Text
How to check whether element has proper color
Maybe not the most elegant way to check element's color if you are using capybara with selenium, but it worked for me pretty well.
page.evaluate_script("$('ul li:first').css('color')").should == "rgb(0, 51, 255)"
0 notes
llptips · 13 years ago
Text
How to use firebug in capybara specs
If you are using rspec with capybara then install capybara-firebug - do the rest as on project wiki. But if you are not using cucumber you need to set in test helper js driver:
Capybara.javascript_driver = :selenium_with_firebug
When you run test, press F12 and firebug will show up.
0 notes
llptips · 13 years ago
Text
When have_content matcher does not work in your capybara tests, or how to check text visibility in other way
Sometimes you need to check in your capybara specs whether some text is, or is not appearing after certain actions. First thing that comes to your mind is, use have_content matcher like this:
find("div.main").should have_content "muahaha"
When it does not work for some reason, use have_xpath matcher:
find("div.main").should have_xpath("//*[text()='muahaha']", :visible => true)
0 notes
llptips · 13 years ago
Text
LessCSS: handle mixins that are not required by markup
By default, all mixins are included by default in the output CSS file, even if they're not used anywhere in the markup. To avoid that, simply add () at the end of the mixin name:
.mixin { // will show up in output CSS as a separate class color:red; } .mixin() { // will only show up in output CSS where it's been used color:red; }
In both cases calling .mixin in other class will work and generate mixin where it's needed, so:
.class { .mixin; }
Outputs:
.class { color:red; }
13 notes · View notes
llptips · 13 years ago
Text
Loading new gems issue in unicorn server
If you ever encounter problem with restarting unicorn server with capistrano (restart with USR2 signal), and you are also using Bundler, then it’s very likely that unicorn server doesn’t load new gems.
This of course results in NameError exceptions in any new code that relies on these gems.
Solution for this is to set BUNDLE_GEMFILE var in before_exec hook in unicorn.rb config file, that points to proper Gemfile. 
before_exec do |server| ENV["BUNDLE_GEMFILE"] = "/path/to/app/current/Gemfile" end
More info here
0 notes
llptips · 13 years ago
Text
SQL needed to migrate Wordpress blog
Just 3 SQL statements are needed when migrating Wordpress blog to new domain. Two if you've used relative links in post content ;-)
1) Site options:
UPDATE wp_options SET option_value = replace(option_value, 'http://www.olddomain.pl', 'http://www.newdomain.pl') WHERE option_name in ('home','siteurl');
2) Post URLs:
UPDATE wp_posts SET guid = replace(guid, 'http://www.olddomain.pl','http://www.newdomain.pl');
3) Internal links within posts that do not use relative URLs:
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.olddomain.pl', 'http://www.newdomain.pl');
0 notes
llptips · 13 years ago
Text
Installing NodeJS on OSX with Homebrew
When you're installing Node via Homebrew, you might get this ugly error:
scons: Building targets ... /usr/bin/llvm-g++ -o obj/release/accessors.o -c -Os -w -pipe -march=core2 -msse4.1 -fno-rtti -fno-exceptions -Wall -W -Wno-unused-parameter -Wnon-virtual-dtor -pedantic -m64 -O3 -fomit-frame-pointer -fdata-sections -ffunction-sections -ansi -mmacosx-version-min=10.4 -DV8_TARGET_ARCH_X64 -DENABLE_DEBUGGER_SUPPORT -I/private/tmp/homebrew-node-0.6.11-fYki/node-v0.6.11/deps/v8/src /private/tmp/homebrew-node-0.6.11-fYki/node-v0.6.11/deps/v8/src/accessors.cc scons: *** [obj/release/accessors.o] Error 127 scons: building terminated because of errors. Waf: Leaving directory `/private/tmp/homebrew-node-0.6.11-fYki/node-v0.6.11/out' Build failed: -> task failed (err #2): {task: libv8.a SConstruct -> libv8.a} DEST_OS: darwin DEST_CPU: x64 Parallel Jobs: 1 Product type: program /usr/bin/llvm-gcc -Os -w -pipe -march=core2 -msse4.1 -rdynamic -pthread -arch x86_64 -g -O3 -DHAVE_OPENSSL=1 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_FDATASYNC=0 -DARCH="x64" -DPLATFORM="darwin" -D__POSIX__=1 -Wno-unused-parameter -D_FORTIFY_SOURCE=2 -IRelease/deps/http_parser -I../deps/http_parser ../deps/http_parser/http_parser.c -c -o Release/deps/http_parser/http_parser_3.o make: *** [install] Error 1
It seems there's a long-standing unresolved error in V8 related to permissions - the installation works just fine when run as root. So let's run it as root:
$ sudo brew install node Password: Cowardly refusing to `sudo brew install'
Nice try... (It won't work with "su" + "brew install node" either.)
So what do we do? We need to convince Homebrew to trust us on this:
$ sudo chown root:staff /usr/local/bin/brew $ sudo brew install node
And this time it works fine. Problem - the whole Node installation is now owned by root, unlike everything else installed by Homebrew. But we can fix this quickly:
sudo chown -R `whoami`:staff /usr/local
And that's it, you can now start your great new chat server :)
9 notes · View notes
llptips · 13 years ago
Text
Dynamic conditions in ActiveRecord associations
There is an ugly hack that enables dynamic conditions in has_one and has_many.
# will be interpolated at class load time :condtions => "posted > '#{Time.now.beginning_of_day.to_s(:db)}'" # will be interpolated when the association is accessed :condtions => 'posted > "#{Time.now.beginning_of_day.to_s(:db)}"'
Hint: second one uses single quotes
1 note · View note
llptips · 13 years ago
Text
How to check if ActiveRecord association was already loaded?
Simply call loaded? on your association
0 notes
llptips · 13 years ago
Text
Ubuntu 11.10 CPU usage issue
I've just upgraded to Oneiric on a Virtual Server and couldn't believe what I was seeing with CPU usage. Maxed out all the time - surely Ubuntu haven't made a mistake??
It looks like there is a regression problem with rsyslogd. There's a helpful blog post detailing the problem and how to downgrade the package at:
http://www.andrewsavory.com/blog/2011/1578
Worth checking ASAP if you upgrade.
0 notes
llptips · 13 years ago
Text
ActiveSupport::Inflector - titleize, humanize
Did you know that humanize (and titleize that calls humanize) strips out the "_id" postfix? I had symbol:
type = :industry_id
and using:
type.to_s.titleize
in the view displayed it as "Industry"
1 note · View note
llptips · 13 years ago
Text
How to rename a MySQL database
Renaming a table in a MySQL database is simple, just call `RENAME TABLE xxx TO yyy`. But what if you want to rename the whole database? There is a [`RENAME DATABASE`](http://dev.mysql.com/doc/refman/5.1/en/rename-database.html) command that you can find in the MySQL documentation, but it has only worked in a few versions until it was removed again, and it's apparently not recommended to use it. Luckily, the recommended method isn't much more difficult: you just need to create a new database and move all tables one by one to the new table. Here's how you do it: echo "CREATE DATABASE newdb" | mysql -u username TABLES=`echo "SHOW TABLES" | mysql -u username olddb | tail +2` for table in $TABLES; do echo "RENAME TABLE olddb.$table to newdb.$table;"; done | mysql -u username This will simply get a list of tables from the `olddb` and call `RENAME TABLE` on each of them to move them to `newdb`.
27 notes · View notes
llptips · 13 years ago
Text
Ignoring local changes to a file in git repository
Let's say you want to modify a file in your repository locally and don't commit that. For example, you might want to use a different `.rvmrc` than the rest of the team, or you might want to disable a non-essential gem or a part of the system that has some problems on your OS. Of course an ideal way would be to fix it in the repo so that it works for everyone, but sometimes that's not possible. You could make the change and just remember not to commit it, but you can be sure you'll forget sooner or later. In this case, the solution is to mark the file as ignored in your local repository. There is something like a "local gitignore", which is located at `.git/info/exclude`, but that won't work if the file is already in the repository. You need to use the `update-index` command instead: git update-index --assume-unchanged .rvmrc The file will just disappear from `git status` and will behave as if you didn't modify it at all. If you change your mind, this is how you remove that "unchanged" flag: git update-index --no-assume-unchanged .rvmrc If you forget which files you've marked as unchanged and you want to see a list, it seems the only way to do that is by using this command: git ls-files -v | grep ^[a-z] This works because files marked as unchanged show up in `ls-files` marked with a lowercase character. (If you know a less hacky way to print that list, let me know...).
6 notes · View notes