Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Last active August 29, 2015 14:20
Show Gist options
  • Save ybur-yug/ea924b12ba3ba77188da to your computer and use it in GitHub Desktop.
Save ybur-yug/ea924b12ba3ba77188da to your computer and use it in GitHub Desktop.
Volt::Tasks AsciiCast

Volt Tasks

Great thing about Volt - reuse between FE and backend. File manipulations and other ops asynch execute and return to frontend simply with promise

Beginning

First off, lets make a new volt app called server_stats

$ volt new server_stats

Let's Make A Task

Now, if we hop into the code we can get to business. Let's say we wanted to display some simple information about Ruby on our machine from the backend server environment. Obviously this would be impossible to do clientside, but it is a perfect candidate for a task in Volt. To create one, we simple use a CLI helper:

$ bundle exec volt g task Stats

This will create a file in our tasks directory with a Volt::Task model that will be able to do the work we need.

To create the task itself, we open up our StatTask model and just begin to add some logic.

class StatsTask < Volt::Task
  def show_stats
  { version: RUBY_VERSION,
    description: RUBY_DESCRIPTION,
    platform: RUBY_PLATFORM }
  end
end

And now in our `main_controller we can run this task for our index view:

...
  def index
    StartTask.show_stats.then do |stats|
      page._info = stats
    end.fail do |err|
      page._info = error
    end
  end
...

and now in main/index.html we can access this in the view code inside <:Body>

  <:Body>
    <h3> Ruby Version </h3>
      <p> {{ page._info._version }} </p>
    <h3> Ruby Description </h3>
      <p> {{ page._info._description }} </p>
    <h3> Ruby Platform </h3>
      <p> {{ page._info._platform }} </p>

Fire up our server with

$ bundle exec volt s

And if we visit localhost:3000 we can see all these stats live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment