Created
August 5, 2015 19:54
-
-
Save werner/26456037c204b8e66495 to your computer and use it in GitHub Desktop.
Ruby on Rails personal shortcuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Remove console logs | |
ActiveRecord::Base.logger.level = 1 | |
#Create a csv file | |
require "csv" | |
CSV.open("path/file_to_create.csv", "wb") { |csv| | |
csv << ["Field 1", "Field 2", "Field 3"]; records.each {|t| csv << [t.field1, t.field2, t.field3.count] } | |
} | |
#Remove duplicates | |
Model.class_eval "def self.dedupe; grouped = all.group_by{|model| [model.field_duplicated_one, model.field_duplicated_two] };grouped.values.each do |duplicates|; first_one = duplicates.shift; duplicates.each{|double| double.destroy}; end; end" | |
Model.dedupe | |
#Creates a sorting order based on another data | |
ModelOne.where("field is not null").order(:field_order).zip(1..x).each {|a,n| a.associated_model.update_all(sorting_order: n) } | |
#Rename files inside a folder and subfolders | |
require 'pathname' | |
Dir['path/*/*'].each {|path| if path=~/.pdf/; file=path.gsub(/(p.*)\/(.*).pdf/,'\\2'); base='new_path'; p=Pathname.new(base + file[0..1] + '/' + file[0..2] + '/' + file[0..3] + '/' + file[0..4]); p.mkpath; File.rename(Pathname.new(path), Pathname.new('new_path' + file[0..1] + '/' + file[0..2] + '/' + file[0..3] + '/' + file[0..4] + '/' + file + '.pdf')) rescue nil; end } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment