Technology
Tech and Tips Thursday: Concerning, Sorting and Planning
Second Edition of our Tech and Tips Thursday series. Today we're talking about creating a concern for loading objects in your controller, exploring an easy-to-use drag 'n' drop library, and discussing how to plan your work better.
Welcome to our second edition of the Tech and Tips Thursday series. Today we will be talking about creating a controller concern to detect which browser a client is using, discuss an awesome drag 'n' drop library and explore the idea of planning your work first thing in the morning.
About T&T Thursday
Tech and Tips Thursday (or T&T Thursday for short) is a biweekly technology and tips article on various software development, technology and productivity topics. Topics that we are interested in have some experience with and topics that we find interesting outside of technology.
T&T Thursday article follows a standard format. It consists of three sections:
- One Coding Example
- One Useful App or Tool
- One Interesting Idea or Thought
One Coding Example
When Rails 4 came out, it included an elegant feature called concerns. A Rails Concern was, in effect, a module that you could include in your model to extract some of the common functionality and share it across your other models.
Concerns were designed to solve three particular problems that many Rails developers tangled with:
- Models with so much code that it was hard to test and understand them.
2. Code duplication, basically copying the same function to other models
3. Make testing easier by separating functionality into testable units.
3. Make testing easier by separating functionality into testable units.
Examples of these included concerns to extract soft-deletion operations into a Trashable concern, a Taggable concern for blog posts and comments, as well as a collection of common ActiveRecord finders for your models.
We've all probably created at least one of these concerns before. But concerns don't stop at models. Concerns are also available for controllers.
Here, we'd like to show you an example of a controller concern.
You've probably created a controller that had a before-action call to load an instance of an object. Something similar to this:
class ProductsController < ApplicationController before_action :load_product, only: [:show, :edit, :update, :destroy] # ... CRUD Actions ... private def load_product @product = current_user.products.find params[:id] end end
And then you'd have something like this for another controller that needed to load a product instance as well.
class ProductCommentsController < ApplicationController before_action :load_product # ... Comments CRUD Actions ... private def load_product @product = current_user.products.find params[:product_id] end end
You can find these little sprinkles of before_action goodness across many applications. How can we use concern to DRY up our code and make it more robust and secure?
Here's the concern you can create.
app/controllers/concerns/with_product.rb
module WithProduct extend ActiveSupport::Concern included do before_action :load_product end def load_product if params[:product_id].present? lookup_id = params[:product_id] elsif params[:id].present? lookup_id = params[:id] end @product = current_user.products.find lookup_id end def redirect_if_product_not_found redirect_to product_not_found_path if @product.blank? end end
You can include the concern shown above in any controller that needs to load a product by ID or product id. Here are two examples.
class ProductsController < ApplicationController include WithProduct before_action :redirect_if_product_not_found, only: [:show, :edit, :update, :destroy] end class ProductCommentsController < ApplicationController include WithProduct before_action :redirect_if_product_not_found end
You can create similar controller concerns for other objects.
One Useful App or Tool
Making things draggable and droppable is a must in today's modern application. All project management apps, many dashboards tools and almost all online games have some drag-n-drop functionality.
There are many libraries out there you can install to add this functionality. For the web, you have to use a javascript library. And one of the easiest to use and most reliable is DragulaJs.
The library creators have a simple tagline that describes how easy it is to use the library.
DragulaJS - Drag and drop so simple it hurts.
And indeed, it is by far the simplest library to set up and use. Here is the list of main features of the library. This list is from their Github page.
- Super easy to set up
- No bloated dependencies
- Figures out sort order on its own
- A shadow where the item would be dropped offers visual feedback.
- Touch events!
- Seamlessly handles clicks without any configuration.
To see how seamlessly it works, check out this demo page.
How do you implement it? Add Dragula to your website using your favourite javascript installation technique (webpacker, node, link tag, etc.). Then instantiate it and add the container whose elements you want to be draggable and droppable.
HTML element
<ul id="dragmearound"> <li>Item 1</li> <li>Item 2</li> </ul>
Javascript
var drake = dragula(); drake.containers.push(document.getElementById("dragmearound"));
That's it.
Check out DragulaJS. You'll be pleasantly surprised.
One Interesting Idea or Thought
"Failing to plan is planning to fail." This quote can be attributed to a variety of individuals, most often to Benjamin Franklin. Somehow we know this, instinctively or because our parents taught us. To succeed at anything, it is best to create a plan first.
But, what does it actually mean to plan?
This is rather a straightforward question to answer. To plan means creating a list of steps or tasks that you need to complete to achieve some goal. It makes complete sense, and we have been doing this for a very long time.
This simple answer is actually missing something. It is missing the essence of planning. It is missing something that can either make or break your plan.
What is that missing piece? Habit.
To plan means to commit to a continual action of deciding what you want to achieve, planning on how you can achieve it and revising your plan when new information comes to you.
The key in that description is the statement about planning being a continual action.
We often create plans and abandon them after steps 3 or 4. This happens for many reasons. You may stop caring about the goal of your plan. Something more important comes. Or it gets too difficult, and you no longer feel like doing it. In other words, life happens.
This is where most people fail with planning. They think of planning as a one-time event. But planning is something you should be doing all the time, every day.
"Failing to plan is planning to fail" is probably being misunderstood by those who read it. This quote is really trying to say that you should be planning all the time; otherwise, you will be failing all the time. Not once. All the time.
At TeamHQ, we plan continually. We have our weekly meetings where we plan our weekly work. We create our tasks on our weekly board. Every morning, we review our plans, rearrange our tasks to reflect our changing priorities, and plan some more.
Our work is affected by new information all the time. There are too many unknowns, but planning daily helps us stay on top and succeed by a little every day.
This is why we are building TeamHQ. To us and you make planning an everyday activity.
To summarize our idea here:
Planning is an essential element of success. Planning every day is an essential element of successful planning.
Until next time.
If you liked this installment of Tech and Tips Thursday, please be sure to subscribe and share it with your friends and colleagues on your favourite platform.