Skip to content

Instantly share code, notes, and snippets.

@codemilan
codemilan / song.rb
Created October 16, 2014 05:03
File size validator- presence true not working
require 'file_size_validator'
class Song < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
validates :attachment, :presence => true,
:file_size => {:maximum => 2.megabytes.to_i}
end
when I submit form without file or empty form it gives error :
@codemilan
codemilan / active_record.rb
Created December 31, 2015 09:46 — forked from jackrg/active_record.rb
Add bulk import functionality to Rails Active Record (add this file to config/initializers, call <model>.import!(array-of-record-hashes))
class ActiveRecord::Base
def self.import!(record_list)
raise ArgumentError "record_list not an Array of Hashes" unless record_list.is_a?(Array) && record_list.all? {|rec| rec.is_a? Hash }
return record_list if record_list.empty?
(1..record_list.count).step(1000).each do |start|
key_list, value_list = convert_record_list(record_list[start-1..start+999])
sql = "INSERT INTO #{self.table_name} (#{key_list.join(", ")}) VALUES #{value_list.map {|rec| "(#{rec.join(", ")})" }.join(" ,")}"
self.connection.insert_sql(sql)
  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros

Custom Date Formats in Rails

Quick guide on printing pretty dates in Rails

# with multiple collections + commands for renaming back the collections
require 'mongo'
# Stop writing to your database.
# Then, for each collection:
# Specify the DB and COLLECTION you want to convert.
# Will insert all modified documents into a new collection
# called 'new_' + old_collection_name.
# Then you can delete the old collection and rename the new one
@codemilan
codemilan / sessions_controller.rb
Created February 9, 2016 11:11 — forked from gonzalo-bulnes/sessions_controller.rb
A SimpleTokenAuthentication-compatible JSON version of Devise::SessionsController. (UPDATE: For a discussion about this gist and a better version of it, please see https://github.com/gonzalo-bulnes/simple_token_authentication/issues/48#issuecomment-42133939)
# app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
# This controller provides a JSON version of the Devise::SessionsController and
# is compatible with the use of SimpleTokenAuthentication.
# See https://github.com/gonzalo-bulnes/simple_token_authentication/issues/27
def create
# Fetch params
@codemilan
codemilan / gist:1acc9e8338f1d534bc74
Created February 25, 2016 05:57 — forked from jingoro/gist:3015664
Mongoid Callback Sequence
require 'rubygems'
require 'bundler/setup'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new('localhost', 27017, :logger => nil).db('mongoid-test')
end
class A
include Mongoid::Document
@codemilan
codemilan / README.md
Created March 11, 2016 04:58 — forked from derwiki/README.md
Ruby module that you can use in a `before_action` on sensitive controllers for which you'd like a usage audit trail

Adding an audit log to your Rails app

If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

  • controller entry points with parameter values
  • permanent information about the user, like user_id
  • transient information about the user, like IP and user_agent

Using the Rails framework, this is as simple as adding a before_action to your admin controllers. Here’s a basic version that I’m using in production.

@codemilan
codemilan / uploads_controller.rb
Created March 17, 2016 05:59 — forked from longlostnick/uploads_controller.rb
Rails JSON file upload with carrierwave (from base64 string)
class Api::UploadsController < ApiController
def create
@upload = Upload.new(upload_params)
ensure
clean_tempfile
end
private