Rails Admin: Conditionally display a field based on a specific object’s value

To conditionally display a field based on a value in the displayed object, you can do the following:

RailsAdmin.config do |config|
  config.model YourObject do 
    edit do 
      field :some_random_attribute do 
        visible do 
          bindings[:object].your_conditional_field=="your_conditional_value"
        end
      end
    end
  end
end

 
In this Rails Admin configuration example, you can see that the YourObject#some_random_attribute field will only display if the YourObject object being displayed satisfies the condition within the “visible” block, otherwise it will not appear.

Rails Admin Custom Dashboard

To create a custom rails_admin dashboard, first create a new file here:

lib/rails_admin.rb

 
And in your rails_config/initializers/rails_admin.rb initializer file, be sure to include the above file:

RailsAdmin.config do |config|
  require Rails.root.join('lib', 'rails_admin.rb')
end

 
Within this new file, do the following:

module RailsAdmin
  module Config
    module Actions
      class Dashboard < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :root? do
          true
        end

        register_instance_option :breadcrumb_parent do
          nil
        end

        register_instance_option :controller do
          proc do
            #You can specify instance variables
            @custom_stats = "grab your stats here."

            #You can access submitted params (just submit your form to the dashboard).
            if params[:xyz]
              @do_something = "here"
            end

            #You can specify flash messages
            flash.now[:danger] = "Some type of danger message here."

            #After you're done processing everything, render the new dashboard
            render @action.template_name, status: 200
          end
        end

        register_instance_option :route_fragment do
          ''
        end

        register_instance_option :link_icon do
          'icon-home'
        end

        register_instance_option :statistics? do
          true
        end
      end
    end
  end
end

 
Now that we have our instance variables, we can customize the actual dashboard HTML by creating the associated file and doing whatever we like.

views/rails_admin/main/dashboard.html.erb

 
This will completely replace the body of the current dashboard statistics page. If you want to maintain some of that functionality, you can always recreate it from the dashboard.html.haml file found within the rails_admin repository.

This is great if you're unsatisfied with the out-of-the-box statistics and want to produce something more robust.

How to set a minimum font size when using CSS3 vw or vh

When using the CSS3 font-size (viewport) units, such as vw and vh, you may have noticed that the fonts can become very small on smaller screens. To resolve this issue, you can do the following:

@media screen and (max-width: 1100px){
  font-size:35px;
}
@media screen and (min-width: 1101px){
  font-size: 3vw;
}

 
Using the media queries above, we can specify a fixed font-size of 35px for screen sizes under 1100px (for instance), and use the viewport unit vw to maintain a responsive font-size for screen sizes greater than 1101px.

This will keep the associated font from becoming unreadable on smaller screen sizes.

MIT’s Eulerian Video Magnification on Ubuntu 14.04

To install the Eurlerian Video Magnification software on Ubuntu 14.04, you’ll want to do the following:

First, download the associated software here:

http://people.csail.mit.edu/mrub/evm/bin/EVM_Matlab_bin-1.1-linux64.zip

Then move the associated zip to your preferred location and unzip.

You will also need the Matlab Runtime environment (not the actual matlab suite), which can be downloaded here:

http://people.csail.mit.edu/mrub/evm/bin/MCR_R2012b_glnxa64_installer.zip

To install the matlab runtime environment, move the zip to your preferred location and unzip. At this point, you should open your terminal (if you haven’t already) and change your directory to the unzipped location and run the following:

sudo bash install

 
Now, open ~/.profile in your favorite editor and add the following lines at the bottom:

export MCR="/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80"

export LD_LIBRARY_PATH=".:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64/client:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v80/sys/java/jre/glnxa64/jre/lib/amd64"

 
Be sure to restart your terminal at this point.

Now, if you were to try to run the reproduce_results.sh script, you’d likely run into the following error:

The file requires the following codec(s) to be installed on your system: video/x-h264

To resolve this issue, run the following commands in your terminal:

sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install gstreamer0.10-ffmpeg

 
At this point, we can process an actual file, so create a /data directory and a /results directory within your EVM_Matlab location and place a video file within the /data directory. Now run the following command to magnify the motion of your video.

./run_evm.sh $MCR data/yourfile.mp4 results/ 30 motion 0.5 10 30 butter 0 80


 Be sure to review the readme file to learn more about the above arguments and additional options.

A cool TED Talk associated with this technology can be found here: See invisible motion, hear silent sounds

You can find more information, including academic papers associated with this functionality, here: Eulerian Video Magnification

Install Android Studio on Ubuntu 14.04

To install Android Studio on Ubuntu 14.04, complete the following steps:

First, download the Android Studio here:

http://developer.android.com/sdk/index.html

 
The Android Studio requires Java’s JDK 6 or above (preferably 7), so you’ll want to do the following at your command line:

sudo apt-get install openjdk-7-jre
sudo apt-get install openjdk-7-jdk

 
Or, if you prefer to use the Android Studio recommended Oracle JDK, you can do the following:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

 
Notice that Ubuntu no longer provides the Oracle JDK as a default installation option, so we need to update our apt repositories to include the webupd8team/java location to retrieve the installer.

Next, go ahead and retrieve the Android Studio Zip file downloaded in the first step and place it in your preferred development location. For instance, we’ve put it in ~/projects/android/

Unzip the file and navigate to

android-studio/bin

 
Run the studio.sh file and complete the initial step to configure the studio.

If the setup guide indicates that you can take advantage of KVM (hardware virtualization), then you can run the following command to see if KVM is activated in your bios:

kvm-ok

 
It should say something like:

INFO: /dev/kvm exists
KVM acceleration can be used

 
If not, then you’ll want to close the setup guide and shutdown your computer to access your bios. Each motherboard is going to be different, but generally the configuration option is probably going to be found somewhere like along these lines (Advanced -> CPU Configuration -> Hardware Virtualization).

Once kvm-ok gives the success message above, you’re good to go and should follow these steps to get everything configured properly:

Intel HAMX instructions

 
The pertinent commands outlined in the link above are the following:

sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
sudo adduser your_user_name kvm
sudo adduser your_user_name libvirtd
#Verify Installation
sudo virsh -c qemu:///system list
#your screen will show ID  Name  State, if everything is successful.

 
At this point you can complete the Android Studio setup guide (android-studio/bin/studio.sh) and install some necessary SDK packages following these instructions:

http://developer.android.com/sdk/installing/adding-packages.html

 
Note that you can find your SDK location in the Android Studio under "Configure -> SDK Manager". Though, it is generally found in /home/user-name/Android/Sdk.

Happy programming.

Rails conditional migrations – ensure column does not exist

If you’re running into a rails migration exception, such as:

column “x” of relation “y” already exists

Then you can conditionally run the associated migration in the following way:

YourModel.reset_column_information
unless YourModel.column_names.include?("the_column")
  add_column(:your_models, :the_column, :boolean, :default=>true)
end

 
Of course, it is probably best to address the underlying workflow issues that caused this issue to occur in the first place. However, if you just want to move along to get along – the above is a great solution.

Check checkbox with tr element

To check a checkbox after clicking a table’s tr element, do the following:

$(document).on("click",".your-tr-element",function(event){
  checkbox = $(this).find("input:checkbox");
  if(event.target.type!="checkbox"){
    if($(checkbox).prop("checked") == true){
      $(checkbox).prop("checked",false);
    }else{
      $(checkbox).prop("checked",true);
    }
  }
});

 
By retaining the event in the callback function, we’re able to ensure the clicked element is not the checkbox itself. This will solve the issue of effectively clicking the checkbox twice and reverting the intended functionality.