Skip to content

Instantly share code, notes, and snippets.

View sandheepg's full-sized avatar

Sandeep Guduru sandheepg

View GitHub Profile
@sandheepg
sandheepg / local-user-management.md
Last active July 28, 2017 12:49
Linux local user management

To get all local users

local-user-management

To add a new user

sudo adduser new_username
@sandheepg
sandheepg / rails-single-model-attribute.md
Created March 10, 2017 13:05
Validate single model attribute in rails

Checking a single attribute on a model for validity doesn’t seem to be possible in Rails. Or at least i couldn’t find a quick answer googling around or looking through the ActiveRecord code.

What I really want to do is pass a hash into valid? with the attributes i want to validate or have an attribute_valid? method. This level of granularity is often useful in AJAX heavy apps. For instance, checking for username availability and validity as the user types during username selection.

Here is a quick solution for one User model.

@sandheepg
sandheepg / localhost-online.md
Last active March 9, 2017 19:01
Access localhost online

To access application hosted locally over the network using HTTP, we can use ngrok

Download ngrok zip file from the website into your local machine and unzip. That gives you an executable file. Navigate to the executable file, and run

ngrok http 80
# OR
./ngrok http 80
# where 80 is the port on which the web server is listening
@sandheepg
sandheepg / php-copyright.md
Created March 9, 2017 05:02
Auto copyright text in footer
@sandheepg
sandheepg / rails-version-error.md
Last active September 26, 2017 17:58
Fixing "uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)"

If you have to work with older versions of Rails, there might be discrepancy between the versions of Rails and Rubygems. You are likely see the below message when you try to boot your rails app.

/home/lt-pc-528/.rvm/gems/ruby-1.8.7-p371/gems/activesupport-2.3.7/lib/active_support/dependencies.rb:55: uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)

You can fix the error by adding the following to environment.rb and script/server.rb files before you require boot.rb file.

@sandheepg
sandheepg / ruby-class-instance-eval.md
Created March 8, 2017 08:00
Ruby class_eval and instance_eval

class_eval is called on a class object and is used to create instance methods for the class at runtime.

%w(administrator developer teamlead tester).each do |user|
  class_eval <<-EOR
    def is_#{user}?
      site == "MYACCOUNT_#{org.upcase}" 
    end
  EOR
end 
@sandheepg
sandheepg / ruby-send.md
Created March 8, 2017 07:59
Using send method in ruby

In Ruby, send is often introduced as a way to show how everything is an object

1.send(:+, 1)  ## -> 2

It can also be used to call private or protected methods from outside a given class as explained below.

class Myclass
  def method_one
@sandheepg
sandheepg / ruby-$:.md
Created March 8, 2017 07:55
$: in ruby

In Rails programming you might seldom come across the below statement

$:.unshift File.dirname(__FILE__)

$: represents load path in Ruby. Its a pre-defined variable and short hand for $LOAD_PATH which ruby uses to look for files.

unshift prepends the given path to the beginning of the array (load path).

@sandheepg
sandheepg / ruby-||-operator.md
Created March 8, 2017 07:54
||= operator in ruby
x ||= y is similar to x || x = y.

In the above statement, if x evaluates to logical true then assignment is not done. If x evaluates to logical false only then the assignment is done.

Though the above two statements are similar, there are subtle differences.

# if x is not defined
@sandheepg
sandheepg / rails-prepend-append.md
Created March 8, 2017 07:50
Difference between prepend and append filters

Note : This is as of rails 2.3.5. The methods might have been deprecated or the behavior might have changed in the latest versions of Rails.

prepend_before_filter and append_before_filter are almost the same.

They differ only in precedence. Prepend filters have higher precedence over append filters.

before_filter is an alias to append_before_filter

class MyController &lt; ActionController::Base