Posts

Easy Way of Finding Coordinates in Polygon

Image
(Ruby on Rails 6 only API, Postgres, Minitest) Hi everybody, Sometimes, we need specific features in our application. One of them may be “How to find Coordinates in Polygon” :) We suppose, we have some areas in our application. We define these areas by using Google Maps and every area has some specific attributes that are name , price, and coordinates. For example, we need information like this, when we click on a point on the map, what are the properties of this point? How to find if the dots are between the corresponding coordinates? GEMS If we are using Ruby on Rails, we think firstly that “is there any Gem about finding these points? Yes, there are some Gems. Like : https://github.com/geokit/geokit https://github.com/apneadiving/Google-Maps-for-Rails https://github.com/alexreisner/geocoder But these gems little huge :) Actually, we don’t need to use completely a gem. Because we just find a point in a polygon, technically. There is a cost of a gem for our a...

More Performance in Cancancan

Image
If you use Ruby On Rails, you probably % 90 know Cancancan gem when you need some authorization and ability. Generally, in the first times experiences everything might be easy. When you start a basic application everything very fast and basic in Rails application. When you added some modules and features can be a little hard to manage your application. After 1 year, if your company or your application still living, you start to think of Rails application performance. You start to fix N+1 problems. You start to improve Active Record Queries. I am seeing to hear your words, why this request takes 5 seconds. :) You start to use the caching mechanism. (For Rails caching  https://guides.rubyonrails.org/caching_with_rails.html ). Run queries by controls and paths And if you start to think, why there are a lot of DB Queries in logs. If you use Cancancan gem, you need caching role queries and some (if) cases according to by request paths, controllers. I’ll try to inform y...

Before save to MongoDB some array fields , you can check from mongo db some validations.

Image
CompanySchema.path("tv_broadcasters").validate(function(value, respond) { var self = this; this.constructor.find( { tv_broadcasters: { $in: value }, _id: { $ne: self.id } }, function(err, company_tvs) { if (err) throw err; company_tvs.forEach(function(tv, key) { console.log("tv_validate : ", tv); console.log("self.id :", self.id); console.log("tv.id :", tv._id); if (self.id === tv._id) return respond(true); return respond(false); }); respond(true); } ); }, "The specified tv_broadcasters is already in use."); https://gist.github.com/muratatak77/bbe743e379add276cf59cc1fc90b7efb

Node JS - Passport module update new user object

passport.serializeUser(function(user, done) { console.log("user serializeUser : ", user); done(null, user); }); req.login(user, function(err) { if (err) return next(err) console.log("After relogin: " + req.session.passport.user); req.session.save(function() { console.info("req_session.save", req.user); passport.deserializeUser(function(user, done) { console.log("user deserializeUser : ", user); done(null, user); }); res.json({ token: token, user: req.user }); }); });

Convert String to Date by using map function in MongoDb

Convert String to Date  by using map function in MongoDb db.getCollection('trackings').find({"created_date": /^Fri Dec 11 2015.*/})       .map(         function(tracking) {           db.getCollection('trackings').update({ "_id": tracking._id},            {             $set: {                 "created_date": new Date(tracking.created_date)             }           },            {             multi: false           }       );      return [tracking.created_date]     }    );

Specific Field Update by Use Map Function

Rails Resque gem 'resque-status' bundle 1. ) app/workers/transcoder.rb class Transcoder include Resque::Plugins::Status @queue = :transcoder def perform puts "transcoder starting..." end end 2.) config/initializers/resque.rb require 'redis' require 'resque/status_server' $redis = Redis.new RS=Resque::Plugins::Status Resque::Plugins::Status::Hash.expire_in = (48 * 60 * 60) # 2 days in seconds 3.) lib/resque.rake require 'bundler/setup' Bundler.require(:default) # require 'nish-news' require 'resque/tasks' task "resque:setup" do ENV['QUEUE'] = '*' Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection } end 4. ) Procfile web: bundle exec rails s -p 3000 worker1: bundle exec rake environment resque:work QUEUE='transcoder' and foreman start https://gist.github.com/muratatak77/bbe743e379add276cf59cc...

Moment JS - Some date functions

exports.getDateByRange = function(req, options) { var start_date = req.param('start_date'); var end_date = req.param('end_date'); var date_type = req.param('date_type') || "yesterday_revenue"; if (date_type == "yesterday_revenue") { options.start_date = moment().subtract(1, 'day').startOf('day')._d; options.end_date = moment().subtract(1, 'day').startOf('day').add({ hour: 23, minute: 59 })._d; } else if (date_type == "week_to_date_revenue") { options.start_date = moment().startOf('week')._d; options.end_date = moment()._d; } else if (date_type == "month_to_date_revenue") { options.start_date = moment().startOf('month')._d; options.end_date = moment()._d; } else if (date_type == "quarter_to_date_revenue") { options.start_date = moment().startOf('quarter')._d; options.end_date ...