#rails3.2
Explore tagged Tumblr posts
codeblocks · 11 years ago
Text
Gems & Libraries in Rails 3.2
Getting up to speed during my first week on the new job. My first task has been upgrading the versions of Ruby and Rails for the main website. To handle some of the common tasks across web applications, we use an in-house gem. While testing the upgrades, I noticed that some of the JavaScript libraries called in the web app weren't being pulled out of the gem. It took some digging to discover why, but here's what I found.
The libraries were previously located in the public/ directory of the gem, which the asset pipeline doesn't check. The asset pipeline will only check the app/assets, lib/assets, and vendor/assets directories.
Solution: hop to the terminal and git mv the directories to lib/assets. Reload the server and the page, check the console, and boom—no more file not found errors.
On the HTML/HAML side, the library is still called in the app with = javascript_include_tag 'jquery-1.10.2.min.js' and we use = javascript_include_tag 'application' to grab all of the other, site-specific stuff. Same with the stylesheets helper. If you know of a more efficient way to do work the asset pipeline in Rails 3.2.17, let me know!
Still more cleaning and tweaking and wrestling of RefineryCMS to do. & don't forget: Friday is Tie Day.
(If you work in the command line, the tree tool is pretty dang awesome, if only for the visualization: brew install tree to get it, so you can see your directories like a tree.)
0 notes
thelatinawholearntocode · 11 years ago
Text
Fav Icon in Rails 3.2
Put the favicon.ico under your public/ folder
Add <%= favicon_link_tag '/favicon.ico' %> to your <head></head>
Create your helper method for favicon_link_tag  under a helpers folder in library with the following code:
Path => File actionpack/lib/action_view/helpers/asset_tag_helper.rb Method: def favicon_link_tag(source='/favicon.ico', options={}) tag('link', { :rel => 'shortcut icon', :type => 'image/vnd.microsoft.icon', :href => path_to_image(source) }.merge(options.symbolize_keys)) end
More info about  favicon (click here)
Or a easier but not that professional way:
Host your image on amazon S3 or any other by your preference 
Add the url of your image in a link 
Add the link  to your head  (under application.html.erb)
Example: <link rel="shortcut icon" href="https://s3-us-west- amazonaws.com/techwomencomingsoonpage/favicon.ico>
0 notes
29stepz-blog · 13 years ago
Text
Rails 3.2+, MongoDB, Dragonfly GridFS file upload
I caught the MongoDB bug recently as part of my own exercise to learn more than 1 database system in my development life somewhat similar to '7 databases in 7 weeks' but less severe regime.
I realised that it is possible to store file uploads within a mongodb instance using its gridfs file system so decided to have a go. I first setup carrierwave and carrierwave-mongoid but it seems that the latest versions of both gems do not work well with the mongoid version 3+ gems due to api changes. After several attempts and google searches I gave up and was in the process of rolling back mongoid gem to 2+ when I suddenly thought of Dragonfly the fantastic file uploader by Mark Evans of new bamboo
One of the coolest features of Dragonfly is the ability to have a single upload such as an image and being able to chain various processing options to it or to scale it on the fly. This beats the carrierwave approach of declaring versions in my opinion
To get this to work I have to do the following:
Declare dragonfly as a gem dependency in your gemfile. Put rack-cache above it if you wish to cache the assets
Create a dragonfly initialiser file documented here in the docs. Now Dragonfly::DataStorage::MongoDataStore has a require for the mongo gem so you need to update your gemfile to also include the mongo, bson and bson_ext gems. I put these before the dragonfly and rack-cache. To configure the datastore I decided to use the more verbose approach and declare everything manually as documented here. 
Within your model declare the relevant accessors. In my case I have a User model which needs to have its own avatar so I added in the following:
field :avatar_image_uid image_accessor :avatar_image
Restart your application server and you should have a working version of file uploads using GridFS with mongodb. Dragonfly also seems to deal with the streaming of the uploads back to the front end without any need to write your own rack middleware to deal with it.
Hope this helps someone with starting with file uploads in mongo. I am still learning about the system so any feedback is greatly welcomed.
2 notes · View notes
2kan-blog · 13 years ago
Text
Liquor. Before a new release.
Started working on Liquor (https://github.com/evilmartians/liquor) again. Hope the new version will be released soon.
What will be done for the new version:
It will be Rails 3.2 compatible.
Since Liquor can't work outside rails a servlet example will be removed.
Also I think it is useless to use Liquor as a Rails template engine, so LiquorView will be removed and using it as a Rails template engine officially will not be supported anymore. If you need it you can do it yourself or send us a pull request, even now it is very clear how to set up your own rails template engine.
Drops syntax will be reviewed and new features will be added.
There will be a few config options added: a default file extension and an ability to define a handling errors strategy, like in a production environment you may want to show only Liquor syntax errors (so the user level ones) and not show any other but report about them to Airbrake.
More tests.
And of course it will be distributed as a gem and we will use Travis CI to be sure that we always have a working version.
And of course bug fixes.
Also I'm working on a templates compilation but this have planned to be released after the stable Rails 3.2 compatible version.
This version will be released ASAP, we also need it in our commercial projects.
Let's talk now about new drops syntax and options.
Don't tell me that the way scopes and attributes were defined wasn't creepy... all that <<, oh! So now it will be more clear:
and
:if and :unless options will be added to the has_one and belongs_to relations.
if an :if option is passed then a relation will be called only if the option evaliates to true.
if an :unless option is passed then a relatopn will be called only if the optopn evaliates to false.
You can pass a method name which will be called on the drop's source object or it could be called on the relation's object if a :remote option is a true, which by default is false. So if your pass a method name and a :remote => true option then a relation object will be fetched, then a method will be called on it (if it is not nil) and if it evaluates to true (for :if and false for :unless) a drop of the object will be returned.
Also you can pass a Proc or any other object which has a call method.
Example:
5 notes · View notes
kazu69 · 13 years ago
Link
2 notes · View notes
fatmodelsblog-blog · 13 years ago
Text
New Rails app with MySQL
rails new [project_name] -d mysql
6 notes · View notes
29stepz-blog · 13 years ago
Text
Client side optimization for Rails 3.2 on heroku
Hi guys
I would like to share some tips with you on how I optimized my own website using techniques recommended by Ryan Bates of Rails casts followed by my own investigation into the subject. If you want to follow this blog post please familiarise yourself with some of the concepts mentioned in the original railscasts which can be found here
Following on the railscasts, I set about running google pagespeed on my own website, 29steps.co.uk. As always, google pagespeed throws up some common issues which I am already aware of. Here's how I overcome the original list of at least 8 high and medium priority items down to just 4 low priority ones:
High / Medium Priority items as reported by google pagespeed:
Combine images into CSS sprites
This means that google pagespeed has detected several images or icons used on the page but are inserted directly into the HTML tags itself. If these images can be combined into a single image and using css sprites to generate classes based on their background position, this warning would go away.
To fix this I use http://csssprites.com/, which allows me to upload the images concerned and concatenate them into a single css sprite. One cool feature of the site is it even generates the css for you with background positions! With this I was able to clear the warning.
I tried using Compass with SASS but for some reason it will not pick up the images to reference in the css so the manual method works. 
You may have to reoptimize the images again - please refer to point 3 below.
Defer parsing of JavaScript
This comes up a lot for me if I use a lot of google plus and other social media javascript embeds in a single page. The plugins for the home page in question that caused this alert are google plus and twitter.
To overcome this issue, I removed the actual javascript embeds from the view template and created a separate js file to autoload the plugins only when the dom is ready, hence 'deferring' the process. The javascript I used is as follows:
This was saved in my assets/javascripts/ folder as 'deferred_main.js' and I use it to load any external javascript files. This is also referenced in application.js so that it can be compiled with it.
Optimize images
This tends to come up on my site regularly after I have added an image to the site. What this means is that the image are not 'lossless' compression and it can be removed by compressing the image before uploading it to the site.
Since I am running imagemagick on my os x, I use the jpegtran command line tool to compress each image to upload:
A more detailed explanation of image optimization can be found below:
http://yuiblog.com/blog/2008/12/05/imageopt-4/
Since I  am using S3 to store the image, I just uploaded the compressed image into the right folder path.
If you are using google pagespeed analyzer within chrome it will provide a link for each image that it has optimized for you. I find in most cases the quality is as good as the original so you can actually download and upload it to your server rather than re compressing it again.
Using HTTP conditionals caching
I noticed that despite resolving some of the high / medium issues above, the site is still slow on first time load. I re ran the test on another site, webpagetest.org 
The reports shows that the First byte load time falls outside the acceptable range of approx 380ms. Note that this value varies for different sites. The report also pointed out that it is a backend issue
Despite contrary belief, starting up another dyno web process does not help. The response time stays the same so it is an issue with the caching mechanism inside refinerycms.
Since I am using RefineryCMS I know page caching is already in use but I decided to add in http conditional caching by extending the page controller through a decorator object:
What the script above does is to cache the page using the page object etag and last_modified_date based on the page object updated_at date attribute. If the page is the same, it will return a cache status of 'fresh' and fetches it from the cache and returns a 304 status message; else it will call the application backend to fetch the template and store it in the cache.
We also need to override the cache_key method inside another decorator for the Refinery::Page model else you will get errors as the original cache_key method is expecting the locale to be passed as an argument - the decorator simply sets a default of 'en' which can also be changed to read from the Refinery::Configuration based on your locale settings.
The above assumes you are already using Rack::Cache with Dalli on heroku. If not please visit the tutorial below to set it up:https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31.
If you are running on rails 3.2 serving your assets statically also cuts down on page load and will also eradicate some of the message from google page speed. Since I am already serving static assets, it makes sense to use the same architecture to use http caching.
Another issue to note is if you are on heroku, it uses Varnish as a reverse proxy. You will need to set the fresh_when and expires_in commands with 'public => true' else it will not work as it is marked as 'private' by default.
Using a CDN
I am not sure how accurate this is by on webpagetest.org, it is recommended to server all static assets such as images and stylesheets through a Cloud Distribution network. Since I am already using S3 I can do so by adding a CDN to the S3 bucket.
If you are looking for a CDN but do not want the hassle of setting one up, try CloudFlare. The starter package is free to use and it uses the DNS settings you provide to set everything up for you - worth a try
If you are running rails 3.2+, the asset pipeline is enabled by default so your javascripts and stylesheets will be compressed and minified hence bypassing the other google pagespeed messages such as 'minifying javascripts' etc. You need to ensure that you regenerate your assets folder inside public before re deploy.
The aftermath
After making those changes above and running some more tests, my overall page speed score has improved to 1.12  and an overall score of 99/100. Here is a link of the report.
The First byte test is still problematic on webpagetest.org which shows it as failing with just one run and passing with 2 runs. I would need to investigate this further.
I did not optimize the actual javascript calls in this instance.
I hope this helps someone with a similar dilemma on trying to improve page load times with your rails 3.2 application running on heroku or your own vps whether or not you are using refinerycms.
1 note · View note
29stepz-blog · 13 years ago
Text
How to check if a remote file exists on an s3 bucket
I keep having to google this all the time so I thought I would remind myself here.
If you are using the AWS-S3 rubygem, you can use the AWS::S3::S3Object instance method exists? with the target bucket name to determine if the object in question exists or not.
For example:
if AWS::S3::S3Object.exists? file_name, destination_target_name # do this if it exists else # do something else since it does not exist end
4 notes · View notes
29stepz-blog · 13 years ago
Text
Using Pygments.rb within Rails 3.2
Snipplets, the syntax highlighting app I built a month ago, makes strong use of Pygmentizer to highlight user submitted code segments. It does so by making a POST request to an external pygmentizer app located on google app engines which then makes a successful request back to the app on completion of the highlighting.
While this serves the purpose of the app, it has some drawbacks. Snipplets relies on a Resque queue to wait for the request to be complete which means the user ends up with an inconsistent view of the codesnipplet; also what happens if the API service fails?
After watching the railscasts episode 207 which introduces pygments.rb I ported all the syntax highlighting code to using pygments.rb.
Whats kool about pygments.rb is that it still uses pygemntizer but the program itself is embedded within the gem and your app using ruby-python and libffi ( through the ffi gem ) which means any environment that supports or runs python can run pygments.rb and that includes Heroku.
However I ran into this peculiar bug whereby pygments.rb crashes the entire application server ( both Mongrel and Webrick as shown in the Railscasts) with a killed signal message on the console when running Pygments.highlight method.
If you are running into the same issue, try switching over to Thin which works for me without any issues. As yet I still cannot ascertain the error whether it is system dependent or not and ruby-debug did not thrown up any unusual bugs. Could have something to do with asynchronous processing but will look into it in a separate post.
If you are new to using pygments.rb and have just switched over you might notice that the line numbers have disappeared completely if you used to have line numbers enabled. This is off by default in the gem and can be enabled by passing the line numbers options to true inside the Pygments.highlight method.
Below is a short snipplet describing some of the workarounds for pygments.rb as discussed above:
The second issue you might run into when deploying onto Heroku is the version of RubyPython you are using. If you get errors such as 'lexer cannot be found' then it is related to RubyPython as pygments.rb cannot pick up on the Python interpreter. To do so simply create an initializer file ( I called mine 'rubypython.rb' and put it inside config/initializers) with the following code:
Essentially what the above does is to start a python interpreter session so that pygmentizer can run. I can't seem to find any other way to avoid this else the entire app will fail. The other gotcha is that it only seems to work with RubyPython 0.5.1 so I have the gemfiles locked down to that version. I tried it with both RubyPython 0.5.3 and the latest 0.6.1 to no avail - the cedar stack on heroku just refuses to communicate with rubypython.
Hope this helps someone with similar dilemma as pygmentizer is a great syntax highlighting tool.
0 notes