#ScheduledResource
This gem is for displaying how things are used
over time -- a schedule for a set of "resources". You
can configure the elements of the schedule and there
are utilities and protocols to connect them:
- Configuration (specification and management),
- Query interfaces (a REST-like API and internal protocols to query the models), and
- A basic Rails controller implementation.
We have a way to configure the schedule, internal
methods to generate the data, and a way to retrieve
data from the client. However this gem is largely
view-framework agnostic. We could use a variety of
client-side packages or even more traditional Rails
view templates to generate HTML.
In any case, to get a good feel in a display like
this we need some client-side code. The gem includes
client-side modules to:
- Manage time and display geometries with "infinite" scroll along the time axis.
- Format display cells in ways specific to the resource models.
- Update text justification as the display is scrolled horizontally.
Configuration
A scheduled resource is something that can be
used for one thing at a time. So if "Rocky & Bullwinkle"
is on channel 3 from 10am to 11am on Saturday, then
'channel 3' is the resource and that showing of
the episode is a resource-use block. Resources
and use-blocks are typically Rails models. Each resource
and its use-blocks get one row in the display. That
row has a label to the left with some timespan visible
on the rest of the row.
Something else you would expect see in a schedule
would be headers and labels -- perhaps one row with
the date and another row with the hour. Headers and
labels also fit the model of resources and use-blocks.
Basic timezone-aware classes (ZTime*) for those are
included in this gem.
Config File
The schedule configuration comes from
config/resource_schedule.yml which has
three top-level sections:
- ResourceKinds: A hash where the key is a Resource and the value is a UseBlock. (Both are class names),
- Resources: A list where each item is a Resource Class followed by one or more resource ids, and
- visibleTime: The visible timespan of the schedule in seconds.
The example file config/resource_schedule.yml
(installed when you run schedulize) should be
enough to display a two-row schedule with just the date
above and the hour below. Of course you can monkey-patch
or subclass these classes for your own needs.
The schedule API
The 'schedule' endpoint uses parameters t1 and
t2 to specify a time interval for the request.
A third parameter inc allows an initial time
window to be expanded without repeating blocks that
span those boundaries. The time parameters
plus the configured resources define the data to be returned.
More About Configuration Management
The ScheduledResource class manages resource and
use-block class names, id's and labels for a schedule
according to the configuration file.
A ScheduledResource instance ties together:
- A resource class (eg TvStation),
- An id (a channel number in this example), and
- Strings and other assets that will go into the DOM.
The id is used to
- select a resource instance and
- select instances of the resource use block class (eg Program instances).
The id could be a database id but more
often is something a little more suited to human use
in the configuration. In any case it is used by model
class method
(resource_use_block_class).get_all_blocks()
to select the right use-blocks for the resource.
A resource class name and id are are joined with
a '_' to form a tag that also serves as an id for the DOM.
Once the configuration yaml is loaded that data is
maintained in the session structure. Of course having
a single configuration file limits the application's
usefulness. A more general approach would be to
have a user model with login and configuration would
be associated with the user.
Installation
Add this line to your application's Gemfile:
gem 'scheduled_resource'
And then execute:
$ bundle
Or install it yourself as:
$ gem install scheduled_resource
Then from your application's root execute:
$ schedulize .
This will install a few image placeholders,
client-side modules and a stylesheet under
vendor/assets, an example configuration
in config/resource_schedule.yml and
an example controller in
app/controllers/schedule_controller.rb.
Also, if you use
$ bundle show scheduled_resource
to locate the installed source you can browse
example classes lib/z_time_*.rb and
the controller helper methods in
lib/scheduled_resource/helper.rb
Testing
This gem also provides for a basic test application
using angularjs to display a minimal but functional
schedule showing just the day and hour headers in
two different timezones (US Pacific and Eastern).
Proceed as follows, starting with a fresh Rails app:
$ rails new test_sr
As above, add the gem to the Gemfile, then
$ cd test_sr
$ bundle
$ schedulize .
Add lines such as these to config/routes.rb
get "/schedule/index" => "schedule#index"
get "/schedule" => "schedule#schedule"
Copy / merge these files from the gem source into
the test app:
$SR_SRC/app/views/layouts/application.html.erb
$SR_SRC/app/views/schedule/index.html.erb
$SR_SRC/app/assets/javascripts/{angular.js,script.js,controllers.js}
and add //= require angular to application.js
just below the entries for jquery.
After you run the server and browse to
http://0.0.0.0:3000/schedule/index
you should see the four time-header rows specified
by the sample config file.
More Examples
A better place to see the use of this gem is at
tv4. Specifically,
models app/models/event.rb and
app/models/station.rb give better examples of
implementing the ScheduledResource protocol and adapting
to a db schema organized along somewhat different lines.
Contributing
- Fork it ( https://github.com/emeyekayee/scheduled_resource/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request