Skip to main content

Adding code to Rails projects other than MVC

I wanted to have development code separate from the M V C of the rails code

so I added a folder lib/rubyscripts





Then in config/application.rb, added this line to load the code there automatically


config.autoload_paths << Rails.root.join("lib/rubyscripts")

After this, I can access code from this area in my controllers etc by using

require "class_name"


What is funny about this setup though is that, unlike in the controllers (where I can just use require "class_name" ) I still have to use the relative location rubyscripts/class_name in my spec files when referring to this code, don't exactly know why!



For example,
spec/lib/rubyscripts/count_finder_spec.rb
@@ -0,0 +1,14 @@
+require "spec_helper"
+require "rubyscripts/countFinder"
+
+# Test for lib/rubyscripts/count_finder.rb
+
+RSpec.describe "CountFinder" do
+ it "calculates counts" do
+ cc = CountFinder.new("http://wikipedia.org/wiki/google")
+ cc.find(["gooGle", ".com", "Yash"])
+ # we expect to find a non zero entry for gooGle but the others we are not sure
+ expect(cc.search_result).to include("gooGle")
+ expect(cc.search_result).not_to include("Yash")
+ end
+end




Comments

Popular posts from this blog

How to write and test Node.JS code that performs mutiple async tasks

This is currently WIP, I will fill in more details but it's a good starting version that is still hopefully useful to the reader... Recently I worked on a project that involved scheduling a number of tasks in parallel in Node.js. Running mutiple tasks I used the await-the library to do this. https://github.com/olono/await-the/blob/master/lib/result.js There are multiple ways to solve this kind of problem. Mainly you have to understand the Node.js is a single threaded environment (See Event loop) and how callbacks operate with a library like async or await-the. Basically, here idea was that you write a function that has a callback as its last argument ```   const doTask = async (arg1, callback) {    const results = await doStuffWith(arg1) ;    callback(results); } ``` Then perform this action on multiple inputs in parallel such as by using the excellent async.mapLimit function if you want to limit how many tasks in parallel you want to to...

Sending Form data to a backend REST API using Axios

This need is incredibly common and useful, and hopefully will save you a lot of time when doing server side calls from your UI application (or even non UI clients like NodeJS applications) Example here is to send a POST request to an endoint /api/item/new (which will create a new item in the database). We will just assume tbhe backend is already setup (it's not relevant to this article). All we need to know is that we can do a POST /api/item/new and send it form data with two pieces of info     name, filter So, if you have a node.js application (I was using Vue-cli generated project, but it does not matter), install 'axios' (a most popular tool to make server calls these days) npm i axios --save OR yarn add axios (my preferred method) Now, in your service JS file (which is generally when I keep all my api calls) do something like this createNew ( name , filter ) { let formData = new FormData (); formData . append ( "name" , ...

React useState returns distinct state values per call

    The React doc states clearly that useState hook returns distinct values, but I still see some devs confused about whether they are working with a shared/global state, when useState is placed inside a custom hook   I put together a very simple codepen to illustrate that each call to the use hook function does indeed get you a distinct piece of state, which is nice! If you really want shared state, hello Redux (or ahem, Context) ! https://codepen.io/binodpanta/pen/vYWdxMd Have fun with React! ⌣