Wednesday, July 13, 2016

Rails Image Upload Using Dragonfly

File uploading is an important feature in web applications. Aside from enabling users to upload profile pictures, the use of file uploading features varies. I have shown you how to enable file uploading in your Rails application using different gems. Today I will be showing you how to do the same using Dragonfly.

Dragonfly is a highly customizable Ruby gem for handling images and other attachments and is already in use on thousands of websites.

You may be given a task to enable file uploading in a Rails application and may not want to make use of the other gems that are out there. You can give Dragonfly a shot, and you will definitely not regret it.

In this tutorial you will create a simple Rails application; I named mine Dragon-Uploader. The application will have just one feature: image uploading.

Installing ImageMagick

To use dragonfly, you need ImageMagick installed on your machine. Follow any of the steps below, depending on your operating system.

Mac Users:

brew install imagemagick

Ubuntu users:

sudo apt-get install imagemagick

Rails Application Generation

rails new dragon-uploader -T

The -T option ensures that your Rails application is generated without the default testing suite.

Go to your Gemfile and add the dragonfly gem.

Do not forget to bundle.

bundle install

Let's generate our controller.

rails generate controller Photos

Integrating Dragonfly

The first step to integrating Dragonfly into your Rails application is to run the dragonfly generation command from your terminal.

rails generate dragonfly

This will create an initializer file for Dragonfly in your config/initializers folder.

The file looks like this:

rails generate model Photo

Dragonfly provides an accessor that you will need to add to your model. With this you can read and write images.

Now navigate to your migration file and add columns.

Note: If you are making use of avatar and not image as I did above, you should change the column to avatar_uid.

Migrate your database:

rake db:migrate

Set up your PhotosController with the necessary actions to upload an image. It should look like this:

You will need to configure your routes.

For now, add routes to the three actions you have created.

You need to set up your views as I have below:

We will come back to these views later.

Validations

For security purposes, you do not want to grant your users the privilege of uploading files of any type. Dragonfly provides you with the necessary methods for this in your initializers.

Now edit your photo model to look like what I have below:

Here is a full list of the validations Dragonfly offers:

You can read more about it in the Dragonfly documentation.

You should also consider giving your users the option to edit their saved images. To do this, we need to add two action methods to our PhotosController and create an edit page in our views. You might want to add the delete and show action while you're at it, as I have below:

If you try to access the show or edit page, you will be presented with errors. This is because we restricted the route to :new, :index, and :update. Now go ahead and change that; it should look like this:

Conclusion

At this point, you can now integrate Dragonfly into your Rails application. Be sure to check out the documentation if you want to try more features not mentioned here. I hope you enjoyed it.

Remember, you can always add feedback, questions, and comments in the form below.


by Kingsley Silas Chijioke via Envato Tuts+ Code

This Week in Mobile Web Development (#116)

Read this on the Web

Mobile Web Weekly July 13, 2016   #116
Chris Brandrick recommends
Mobile Web Applications: Top CSS Techniques for Optimal Performance — Building mobile web applications with great performance can be hard. So here’s a look at how using current techniques and leveraging HTML5 and CSS3 can result in powerful ‘native-like’ experiences.
Dinis Carvalho
Brian Rinaldi recommends
The Search For The Holy Grail: How I Ended Up With Element Queries, And How You Can Use Them Today — A discussion of the history and uses of element queries and how this led to the creation of EQCSS, a plugin that supports element queries, Scoped CSS, and more.
Tommy Hodgins
Peter Cooper recommends
ZingTouch: A JavaScript Mobile Gesture Detection Library — Provides six standard gestures that are completely customizable (Try it on a touch-enabled device).
ZingChart
Sponsored
Dropsource - Turn Your Web App Into A Native App with New Visual App Builder — Want to easily turn your web apps and APIs into truly native iOS and Android apps? With Dropsource, you can. Our visual app platform that makes building truly native iOS Android apps easier, faster, and more convenient. Join free today.
Dropsource

Brian Rinaldi recommends
Hybrid or Native Mobile App? Use the Right Tool for the Job — When should you use hybrid apps and when should you go native? Learn about the differences and which is best for your mobile app development.
Rob Lauer
Peter Cooper recommends
Browse eBay with Style and Speed — A look at efforts being made by eBay to improve users’ experiences when accessing their site on mobile devices.
Senthil Padmanabhan
Holly Schinsky recommends
3 Simple Steps to Improve Your React Redux Code — Useful tips on how to achieve an organized and more scalable setup for your React/Redux apps.
Cheesecake Labs
Chris Brandrick recommends
Complexion Reduction: A New Trend in Mobile Design — Flat, Clean, Minimal, Material..what’s next?
Michael Horton
Brian Rinaldi recommends
Developer Economics: State of Developer Nation Q1 2016 — A survey of over 21k developers offers some insights into how they are currently targeting mobile and devices.
VisionMobile
Brian Rinaldi recommends
NativeScript Developer Day 2016 — A 1 day conference (with a pre-conference workshop) focused on native mobile development with JavaScript, AngularJS and the NativeScript open source platform.
Progress
Brian Rinaldi recommends
What's New in NativeScript 2.1 — 2.1 includes load time and runtime optimizations on Android, tooling improvements and more.
NativeScript.org
Holly Schinsky recommends
Working with Ionic Native - Shake, Rattle, and Roll — Ray talks about Ionic Native (the successor to ngCordova) and includes a sample app showing how to use it with the Cordova Device Motion plugin.
Raymond Camden
Peter Cooper recommends
Build a Hybrid Mobile App with Angular 2 and Ionic 2 — A very thorough walkthrough.
Ganga Chris
Sponsored
Job Offers. No resume necessary. — Create your Hired profile to get top companies to start applying to hire you. Get offers from $75,000 - $250,000 on the platform in 1 week.
Hired.com

Holly Schinsky recommends
PluginPub - Publish Your PhoneGap Plugins to NPM — PluginPub is a tool to help automate publishing your Cordova/PhoneGap plugins to NPM.
Simon MacDonald
Holly Schinsky recommends
Creating a Cordova Hybrid App with React, Redux and Webpack — How to create a Cordova hybrid weather app that uses Onsen UI, React and Redux to manage state.
Onsen UI
Brian Rinaldi recommends
How Do Mobile Emulators Work? — Jen Looper explains the technology behind iOS and Android mobile emulators and why they function so differently.
Telerik Developer Network
Curated by Brian Rinaldi and Holly Schinsky for Cooper Press.
Cooper Press is located at Office 30, Fairfield Enterprise Centre, Louth, LN11 0LS, UK
Update your email address
or stop receiving MWW here


by via Mobile Web Weekly

Images Grid jQuery Plugin

Images grid jQuery plugin.Display first 6 images in grid view.Rest of images can be viewed in modal box.

The post Images Grid jQuery Plugin appeared first on jQuery Rain.


by Admin via jQuery Rain

Rake 201

In this second article about Rake, we dive a little deeper and cover slightly advanced topics like file tasks, rules, multitasks and more that will improve your Rake chops significantly.

Topics

  • Disclaimer
  • Default Task
  • The Task Object
  • File Tasks
  • Directory Method
  • File Utilities
  • Rules
  • Trace Flag
  • Parallel Tasks

Disclaimer

I want to approach this topic from a more general point of view. This is not an article that shows you a list of Rake tasks with clever solutions that are ready to be copied and pasted without much afterthought. It’s more intended to be a look under the hood while being newbie-friendly and also interesting to people who just haven’t yet played much with Rake besides the obvious Rake tasks in Rails. 

It’s the understanding of the tool and what it offers that yields a higher return, I think. I hope you don’t mind. To me, covering the principles is more valuable—and more approachable to beginners—and it’s up to you what you do with it in your own applications.

Default Task

I’m sure you have at least heard the term previously somewhere. But what is a default task really? It’s nothing magic, but let’s get this one out of the way quickly. When you run rake without any additional name for a rake task, the default task gets executed.

Shell

Some Rakefile

In Rails, the default task is supposed to run your tests. Your guess is as good as mine, but I suppose it was a result of tests needing to be run more often than any other task. When you redefine the default Rake task in Rails, it just adds up to the task defined by Rails—it won’t redefine it. It’s actually how Rake works. When you redefine a Rake task, you add up to the previous definitions.

File Tasks

As the name might suggest, they are tasks that you execute on files (and directories). They have a few tricks up their sleeves, though. Rake will, of course, work a lot of the time with files. No surprise that somebody recognized that pattern and created specialized file tasks for your convenience—especially for the simple reason to avoid duplication or wasting processing capabilities.

Transforming files from one type to another is a very common task. The sources are your dependencies and the task names are what follows the file keyword. Converting Markdown to HTML files, converting HTML files into ebook formats, JPG images to PNG images, compiling source code, building static pages or just changing file extensions and many more options are at your disposal. We could do all of this manually, but this is tedious and ineffective, of course. Writing code for this is much more elegant and scalable.

Using file tasks is not much different from “regular” tasks. They will also show up if you ask for a list of Rake tasks via rake -T. In fact, Rake treats all tasks equally—except multitask a bit. Adding descriptions and prerequisites is no problem for file tasks to handle as well. 

Actually, prerequisites are a necessity for mentioning source files before they get processed. We need the source to exist for this to work—which makes sense as a dependency, of course. Without it, Rake wouldn’t know how to continue—it can’t create the new file out of thin air, after all.

Some Rakefile

The name for your file task is basically your target file, the file that you want to have created. The prerequisite is the source file that is needed for the task. Inside the block, you are telling Rake how to create the desired output—how to build it using the prerequisite file(s) that already exist. Input-output. For example, this could be a shell command using the pandoc tool that transforms Markdown files into HTML files. The applications for file tasks are more than plenty. The syntax, though, might feel a little weird at first. I get it.

Rake first checks if the target file exists and, if so, checks if the timestamp is older than the prerequisite files—a time-based dependency. Rake will run the file task if the timestamp is older than the prerequisites or if the file does not exist yet. That is very handy if you need to handle more than a couple of files—which is especially cool because you won’t need to rebuild a ton of files just because you changed a single one in a collection, for example. In contrast to that, regular Rake tasks are always run—they don’t check any timestamps or other changes, unless you make them so, of course.

File Utilities

Some Rakefile

Shell

In case you are wondering about the cp method in the previous example or the above mv command, let’s talk about file utilities. We could have used sh mv ... to execute a Shell command from within a Rake task. Luckily for us, we can use a module that makes Shell command stuff like this a lot less verbose and platform independent. FileUtils is a Ruby module with lots of unixy commands for file operations: 

  • rm 
  • cp 
  • mv
  • mkdir
  • and so on…

If reinventing the wheel is not your thing, FileUtils will be a useful companion dealing with files. Often Rake is all you need, but every once in a while, you’ll be really glad this handy module has got your back. RakeUtils extended this module slightly for your convenience. 

Let’s have a look at a list of what’s at your disposal and then zoom in on a few particular ones that might be of interest to you:

Although I assume that you are a newbie, I also assume that you have played with Rails before and that you know the very basic Unix utilities—stuff like mv, cd, pwd, mkdir and stuff. If not, do your homework and come back. 

In your Rakefiles, you can use these methods right out of the box. And to avoid misunderstandings, this is a Ruby layer that ‘imitates’ these Unix commands and which you can use in your Rakefiles without any prefixes like sh—for executing a Shell command. By the way, the options you see in the list above mean a hash {} of options. Let’s look at a few interesting commands that might come in handy writing file tasks:

  • sh

This lets you execute shell commands from within your Ruby files.

  • cd

This is a very basic one, but there is something cool about this command. If you provide cd with a block, it changes the current directory to its destination, does its business as defined in the block, and then returns to the previous working directory to continue. Neat, actually!

  • cp_r

Lets you copy files and directories recursively in bulk.

  • mkdir_p

Creates a target directory and all its specified parents. Luckily for us, we have the directory method in Rake, which is even more convenient, and therefore we don’t need it.

  • touch

This updates the timestamp of a file if it exists—if not, it gets created.

  • identical?

Lets you check if two files are the same.

Directory Method

In Rake, you have a handy way to define directories without using mkdir or mkdir_p. It’s especially handy when you need to build up nested directories. A folder tree can be a pain if you need to build up a directory structure via multiple file tasks that have tons of prerequisites for the directory structure. Think of the directory method as a folder task.

Some Rakefile

This creates the directories in question without much fuss. What might not be obvious right away is the fact that you can depend on it like any other rake task—as a prerequisite. Just make sure the name of the file task, its name, includes the directory that you depend on. If multiple tasks depend on it, it will still get created only once.

As you can see here, Rake is very consistent and thinks about all the things to build as tasks. Thanks, Jim, that makes life easy!

Rules

Rules can help us to reduce duplications when we deal with tasks—file tasks, actually. Instead of instructing Rake to execute tasks on particular files like somefile.markdown, we can teach Rake to execute these tasks on a certain kind of file—like a pattern or blueprint. Transforming a set of files instead of single ones is a much more versatile and DRY approach. Tasks like these scale much better when we define a pattern for files that share similar characteristics.

Some Rakefile

As you can see, having a bunch of files would be tedious to maintain that way. Yes, we can write our own script where we keep a list of files in an array and iterate over it, but we can do better—much better. 

Another unwanted side effect would be that every time we run such a script, all HTML files get rebuilt—even if they haven’t changed at all. A large list of files would make you wait a lot longer or take up a lot more resources than necessary. We don’t need any extra code to take care of multiple files. Rake does a better, more efficient job in that department since it only executes its file tasks or rules when the file was touched in the meantime.

Some Rakefile

When we define a rule like the above, we have a mechanism in place for transforming any file with a .markdown extension into an .html file. With rules, Rake is first looking for a task for a specific file like quartermaster_gadgets.html. But when it can’t find one, it is using the plain .html rule to look for a source that could achieve successful execution. That way you don’t have to create a long list of files but only use a general “rule” that defines how to handle certain file tasks. Pretty awesome!

Task Object

In the rule above, we were making use of the task object—in this case a rule object, to be even more precise. We can pass it as a block argument into the closure and call methods on it. As with file tasks, rules are all about task sources, its dependencies—a markdown file, for example—and its task name. 

From within rules’ body in the block (and file tasks), we have access to the rules’ name and source. We can extract information from that argument passed—the name through rule.name and its source (aka file source) via rule.source. Above, we could avoid duplicating the names of the files and generalize a pattern instead. Similarly, we could get the list of prerequisites or dependencies with rules.prerequisites. For file tasks or any other task, the same applies, of course.

Talking of dependencies, they can function like a list to be iterated. There's no need to create a separate each loop if you play your cards right. 

As you can see, we didn’t need to manually iterate over the list of articles. We simply put Rake to work and used the dependencies—which is a lot simpler and cleaner.

What is even cooler for DRYing stuff up is that rules can take a proc object—an anonymous function object, a lambda basically—as a prerequisite. That means that instead of only a single pattern as a prerequisite, we can pass something more dynamic that lets us cast a net of patterns that catches more than a single fish. For example, rules for .markdown and .md files. 

They would have the same body of the rule but only a different pattern as prerequisite. It’s like defining a new File task for every object returned by the proc object. Another way to work with rules is regular expressions, of course. You pass a pattern as a dependency and if you got a match, the file task can be executed. Sweet options, no?

The Difference Between Procs and Lambdas

If you are new to lambda land or haven’t figured it out completely yet, here is a small refresher. Procs are objects you can pass around that can be executed later—so are lambdas. Both are Proc objects, by the way. The difference is subtle and comes down to the arguments that are passed into them. Lambdas check the number of arguments and can blow up with an ArgumentError for that reason—procs don’t care. The other difference is in regards to their handling of return statements. Procs get out of the scope where the proc object was being executed. Lambdas just exit the lambda scope and continue to trigger the next code that is in line, so to speak. Not super important here, but I thought for the newbies among you, it can’t hurt either.

Useful Flags

This is a short list of flags that you can pass to rake tasks.

  • --rules

Shows you how Rake tries to apply rules—a trace for rules. Invaluable if you deal with a couple of rules and run into bugs.

Shell

  • -t

Remember the solve_bonnie_situation task from article one? Let’s add this flag to this Rake task and turn on tracing. We also get a backtrace if we run into errors. This is certainly handy for debugging.

Shell

Tracing Rules

Setting Rake.application.options.trace_rules = true in a Rakefile itself tells Rake to show us trace information about rules when we run a task. This is cool because when we run a trace via rake -t, with a single flag, we get all the debugging information we need. We not only get a list of task invocations but can also see which rules were applied—or attempted.

  • -P

Shows a list of prerequisites for all tasks. Here we use again the solve_bonnie_situation task. Omitting output for other tasks, this would be its singled out output:

Shell

If you are curious, run rake -P. Pretty interesting output.

  • -m

Runs tasks as multitasks.

Parallel Tasks

Let me introduce you to the multitask method. This can help you to speed things up a bit—after all, we have multiple cores on most modern computers, so let’s make use of them. Of course, you can always achieve the speed boosts by writing solid code that lacks any fat, but running tasks in parallel can certainly give you something extra in that regard. There are pitfalls, though, which we will also cover.

The tasks we executed so far run all tasks in sequence, one after the other. It’s a safe bet if your code is in order, but it is also slower. If speed is important for some task, we can help a little by multi-threading tasks. Keep in mind, though, that the sequential approach is the better option under some circumstances.

Let’s say we have three Rake tasks that need to run as a prerequisite in order to execute a fourth one. These are four threads, basically. In the bigger picture of things, when you run multiple applications—or to be more specific, processes—at once, the same idea is at work.

Using multitask, the dependencies in our prerequisite array are now not executed in this order anymore. Instead, they are spread and run in parallel—but before the shoot_bond_movie task, of course. A Ruby thread for each task will be run at the same time. Once they are finished, shoot_bond_movie will do its business. The way tasks are acting here is similar to randomization, but in fact they are simply executed at the same time.

The tricky part is just to make sure that certain dependencies are processed in an order that suits your needs. Because of that, we need to take care of race conditions. This basically means that some task runs into problems because the order of execution had unintended side effects. It’s a bug. 

If we can avoid that, we achieve thread safety. In regards to common prerequisites, interestingly, these prerequisites will be run only once because the multitask prerequisites wait for their completion first.

Both shoot_car_chase and shoot_final_confrontation tasks depend on prepare_italy_set to finish first—which is only run once, by the way. We can use that mechanism to predict the order when running tasks in parallel. Don’t just trust the order of execution if it’s somehow important to your task.

Final Thoughts

Well, I guess you are now fully equipped to write some serious Rake business. Making proper use of this tool will hopefully make your life as a Ruby developer even more joyful. In this second article, I hope I could convey what a simple but marvelous tool Rake really is. It was created by a true master of his craft.  

We all owe Jim Weirich tremendous respect for coming up with this elegant build tool. The Ruby community is certainly not quite the same since he passed away. Jim’s legacy is clearly here to stay, though. Another giant we are privileged to build upon.


by Ed Wassermann via Envato Tuts+ Code

6 Browser Extensions for Busy Social Media Marketers

gd-marketer-browser-extensions-600

Do you need to increase your productivity? Looking for browser extensions that simplify your marketing? In this article, you’ll discover six browser extensions to help busy marketers. #1: Group Multiple Tabs Into a Single Tab With OneTab Some of the best tools are the simplest ones, and you can’t get much simpler than OneTab. When [...]

This post 6 Browser Extensions for Busy Social Media Marketers first appeared on .
- Your Guide to the Social Media Jungle


by Greg Daily via

Chordsurfer

Discover a world of music and joy, surf into Red Bull TV's Season of Festivals, an infinite runner game made with WebGL.
by via Awwwards - Sites of the day

Tuesday, July 12, 2016

8 Common Yet Nasty Mistakes You Must Avoid When Writing Content


Things become challenging when it comes to writing for the web users. Besides informative, your piece of writing should be engaging and according to the taste of your target audience.

But there are certain mistakes that make your content less appealing to the reader. And the irony is that you don’t even realize what your mistakes are. Here, we have outlined common yet ghastly mistakes that you must keep away from your content. Let’s see.

by Guest Author via Digital Information World