Senior Software Engineer: Love Writing and Reading Software, Taking Entrepreneurial Risks, Teaching Computer Programming & Blogging here or in many other places. I also Love Music and I can call myself a small pianist!
Twitter Bootstrap 2 has a bug while on modal dialogs. The page does not scroll as expected. So, if the browser window is not wide/tall enough to display the whole
modal dialog content, the scroll bars do not make it appear.
This problem does not occur with jQuery modals neither with Twitter Bootstrap 3.
I am using Ruby on Rails and the less-rails-bootstrap gem.
In order to fix the problem for my current application, and instead of moving to Twitter Bootstrap 3 or to jQuery, I have decided to fix the problem in the gem
Important Note This bug influences your capybara/selenium tests, because elements that are normally visible, while the browser is maximized, they cannot be located by
selenium and they are reported as non-visible and cannot be interacted with, if the window browser is not open enough to display the whole modal dialog.
I usually come across people asking what is the preferred way of laying out the properties of a model and, in fact, of an ActiveRecord model.
Shall we put first the validations and then the associations? Or first the associations and then the validations? Or shall we put first the
callbacks?
One can google for rails style guide. Will basically find only this:
# Here, I am defining a "module" with name Ma. I also define two methods. One without "self."# and one with. See, later on, what happens when I "include" and what happens when I "extend"# the "module" within a "class".#moduleMa# I will be able to make this method an instance or a class method of a class.# It depends whether I will "include" or "extend" the module in the class.# Note that this method, I cannot call it directly on Ma. In order for this method# to be useful, I have to include or extend this module within a class.#defma_method1puts"ma_method1"end# This method, is not reusable, in the sense that I cannot make it be an instance or class# method of a class. But still, it is a method of module Ma and I can call it directly.#defself.ma_method2puts"ma_method2"endendputs"Ma responds to ma_method1? : #{Ma.respond_to?(:ma_method1)}"# it will print "false"puts"Ma responds to ma_method2? : #{Ma.respond_to?(:ma_method2)}"# it will print "true"puts"-------------"classA# "include" sets the module methods as instance methods of the classincludeMaenda=A.newputs"a Responds to ma_method1?: #{a.respond_to?(:ma_method1)}"# it will print "true"puts"A Responds to ma_method1?: #{A.respond_to?(:ma_method1)}"# it will print "false"puts"a Responds to ma_method2?: #{a.respond_to?(:ma_method2)}"# it will print "false"puts"A Responds to ma_method2?: #{A.respond_to?(:ma_method2)}"# it will print "false"puts"-------------"classB# "extend" sets the module methods as class methods of the classextendMaendb=B.newputs"b Responds to ma_method1? : #{b.respond_to?(:ma_method1)}"# it will print "false"puts"B Responds to ma_method1? : #{B.respond_to?(:ma_method1)}"# it will print "true"puts"b responds to ma_method2? : #{b.respond_to?(:ma_method2)}"# it will print "false"puts"B responds to ma_method2? : #{B.respond_to?(:ma_method2)}"# it will print "false"puts"-------------------"
But you can also include or extend a module in another module too. Read this gist by
pglombardo who has kindly appended to the above
piece of code.
There are sometimes that you might want to use class-level variables in a reusable module. How will you do that? Here is a sample ruby program that does that:
In the above example, we keep a class level Set of symbols (@cached_keys). We want every class that mixes in Cacheable to have its own instance of that Set.
I have been using Solr the last two years for quite some projects. My work was always done using sunspot
(via sunspot_rails when one has Rails application), a gem that is a client library for Solr.
You need to know few things about Solr, when using sunspot to access it and do most of the things. sunspot is a fantastic gem and relieves you from
huge amounts of work when storing data in a Solr database.
However, there are times that I felt that I needed to know more. For example, when I was searching for University of Athens and I got no results, but I knew
that my documents had all or any of the words “University”, “of”, “Athens” in their body, or, even worse, there were also documents that were having the whole
phrase “University of Athens” in their body. Or I wanted to query with parts of words, or parts of e-mails, and still I was not getting any results. You can see how
I ended up solving these problems reading these two: 1) Why Sunspot Solr does not bring results when I include “of” word in search query?
2) How can I set Sunspot to search for sequences of characters instead of words?
Hence, I decided to take a journey to Solr. And this will be a registration of my steps to this journey. Understanding how Solr works, will make me manage
better what I can do and achieve with sunspot too.
One cannot be but extremely impressed of the features and capabilities of Solr. I believe that I will use to replace many of my MySQL stores that are only
used for querying.
Did you know that you can execute migration commands from rails console? Assume that you want to change the column ‘name’ of the ‘Product’ model, to have a limit of 32 characters and set it to not null. Here is what you can do:
Example of ActiveRecord Migration run from Rails Console
Capistrano is a great tool for deploying Ruby on Rails applications on remote server machines. If you have ever used it, you may already know that it creates new directory structures for each new release that it realizes and sets “current” to point to the latest one.
This is very fine, but there are cases in your application that you might want to share the same directory data among releases. You can declare in your “deploy.rb” file the directories that will be shared among your releases. By default, the directories “log”, “public/system” and “tmp/pids” are already shared. Hence, you do not have to do anything for them. But, if, for example, you want to share the “my_custom_data” directory that you have inside your application structure, you need to add the following line inside your “deploy.rb” file:
Please, note that my_custom_data should pre-exist while you are doing the deployment. But, bundle exec cap setup will create shared folders anyway for you the first time you setup your servers.
I consider testing one of the most important phase in application development and Rails does a very good job on that. However, testing documentation on Rails Guides is still work under development.
Here is a short tutorial on how you can test that a reponse has rendered the correct template and the correct layout.
If you want to make sure that the response rendered the correct template and layout, you can use the assert_template method:
assert_template usage
12345
test"index should render correct template and layout"doget:indexassert_template:indexassert_template:layout=>"layouts/application"end
Note that you cannot test for template and layout at the same time, with one call to assert_template method. Also, for the layout test, you can give a regular expression instead of a string, but using the string, makes things clearer. On the other hand, you have to include the “layouts” directory name even if you save your layout file in this standard layout directory. Hence,
This will not work
1
assert_template:layout=>"application"
will not work.
Gotcha: Watch out if your view renders any partial
If your view renders any partial, when asserting for the layout, you have to assert for the partial at the same time. Otherwise, assertion will fail.
Hence:
Correct way to assert for the layout
1234
test"new should render correct layout"doget:newassert_template:layout=>"layouts/application",:partial=>"_form"end
is the correct way to assert for the layout when the view renders a partial with name_form. Omitting the :partial key in your assert_template call will complain.