## 1. Project Overview & Quickstart (bbatsov/CODE_OF_CONDUCT)

# CODE_OF_CONDUCT

Open-source repository bbatsov/CODE_OF_CONDUCT

### Repository Details
- **Repository:** [bbatsov/CODE_OF_CONDUCT](https://github.com/bbatsov/CODE_OF_CONDUCT)
- **Primary Language:** Code

*Note: High-volume repository documentation is actively indexed and synchronized by YakaAI.*

## 2. Official Technical Reference & Guides (bbatsov/docs)

## File: en/contributing/README.md

# Contributing

You want to contribute?  Great!  Thanks for being awesome!  We have full docs on how to contribute here:
https://github.com/voltrb/volt/blob/master/CONTRIBUTING.md

---

## File: en/deployment/README.md

# Volt Deployment

This chapter highlights various deployment options for Volt on popular cloud providers.

---

## File: en/docs/arraymodel_events.md

## ArrayModel Events

Models trigger events when their data is updated.  Currently, models emit two events: ```added``` and ```removed```.  For example:

```ruby
    model = Volt::Model.new

    model._items.on('added') { puts 'item added' }
    model._items << 1
    # => item added

    model._items.on('removed') { puts 'item removed' }
    model._items.delete_at(0)
    # => item removed
```

---

## File: en/docs/assets.md

## Assets

**Note, asset management is still early, and likely will change quite a bit**

In Volt, assets such as JavaScript and CSS (or sass) are automatically included on the page for you.  Anything placed inside of a components asset/js or assets/css folder is served at /assets/{js,css} (via [Sprockets](https://github.com/sstephenson/sprockets)).  Link and script tags are automatically added for each css and js file in assets/css and assets/js respectively.  Files are included in their lexical order, so you can add numbers in front if you need to change the load order.

Any JS/CSS from an included component or component gem will be included as well.  By default [bootstrap](http://getbootstrap.com/) is provided by the volt-bootstrap gem.

**Note: asset bundling is on the TODO list**

---

## File: en/docs/attribute_bindings.md

# Attribute Bindings

Bindings can also be placed inside of attributes.

```html
<p class="{{ if _is_cool? }}cool{{ end }}">Text</p>
```

There are some special features provided to make elements work as "two way bindings":

```html
<input type="text" value="{{ _name }}" />
```

## CheckBoxes

In the example above, if ```_name``` changes, the field will update, and if the field is updated, ```_name``` will be changed:

```html
<input type="checkbox" checked="{{ _checked }}" />
```

If the value of a checked attribute is ```true```, the checkbox will be shown checked. If it's checked or unchecked, the value will be updated to ```true``` or ```false``` respectively.

## Radio Buttons

Radio buttons bind to a checked state as well, except instead of setting the value to true or false, they set it to a supplied field value.

```html
<input type="radio" checked="{{ _radio }}" value="one" />
<input type="radio" checked="{{ _radio }}" value="two" />
```

When a radio button is checked, whatever checked is bound to is set to the field's value.  When the checked binding value is changed, any radio buttons where the binding's value matches the fields value are checked.  NOTE: This seems to be the most useful behavior for radio buttons.

## Select Boxes

Select boxes can be bound to a value (while not technically a DOM property, this is another convenient behavior Volt adds).

```html
<select value="{{ _rating }}">
  <option value="1">*</option>
  <option value="2">**</option>
  <option value="3">***</option>
  <option value="4">****</option>
  <option value="5">*****</option>
</select>
```

When the selected option of the select above changes, ```_rating``` is changed to match.  When ```_rating``` is changed, the selected value is changed to the first option with a matching value.  If no matching values are found, the select box is unselected.

---

## File: en/docs/automatic_model_conversion.md

## Automatic Model Conversion

### Hash -> Model

For convenience, when placing a hash inside of another model, it is automatically converted into a model.  Models are similar to hashes, but provide support for things like persistence and triggering reactive events.

```ruby
    user = Volt::Model.new
    user._name = 'Ryan'
    user._profiles = {
      twitter: 'http://www.twitter.com/ryanstout',
      dribbble: 'http://dribbble.com/ryanstout'
    }

    user._name
    # => "Ryan"
    user._profiles._twitter
    # => "http://www.twitter.com/ryanstout"
    user._profiles.class
    # => Volt::Model
```

Models are accessed differently from hashes.  Instead of using `model[:symbol]` to access, you call a method `model.method_name`.  This provides a dynamic unified store where setters and getters can be added without changing any access code.

You can get a Ruby hash back out by calling `#to_h` on a Model.

### Array -> ArrayModel

Arrays inside of models are automatically converted to an instance of ArrayModel.  ArrayModels behave the same as a normal Array except that they can handle things like being bound to backend data and triggering reactive events.

```ruby
    model = Volt::Model.new
    model._items << {name: 'item 1'}
    model._items.class
    # => Volt::ArrayModel

    model._items[0].class
    # => Volt::Model
    model._items[0]
```


To convert a Volt::Model or a Volt::ArrayModel back to a normal hash, call .to_h or .to_a respectively.
To convert them to a JavaScript Object (for passing to some JavaScript code), call `#to_n` (to native).

```ruby
    user = Volt::Model.new
    user._name = 'Ryan'
    user._profiles = {
      _twitter: 'http://www.twitter.com/ryanstout',
      _dribbble: 'http://dribbble.com/ryanstout'
    }

    user._profiles.to_h
    # => {twitter: 'http://www.twitter.com/ryanstout', dribbble: 'http://dribbble.com/ryanstout'}

    items = Volt::ArrayModel.new([1,2,3,4])
    # => #<Volt::ArrayModel:70226521081980 [1, 2, 3, 4]>

    items.to_a
    # => [1,2,3,4]
```

You can get a normal array again by calling .to_a on a Volt::ArrayModel.

---

## File: en/docs/buffers.md

## Buffers

Because the store collection is automatically synced to the backend, any change to a model's property will result in all other clients seeing the change immediately.  Often this is not the desired behavior.  To facilitate building [CRUD](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete) apps, Volt provides the concept of a "buffer".  A buffer can be created from one model and will not save data back to its backing model until .save! is called on it.  This lets you create a form thats not saved until a submit button is pressed.

```ruby
    store._items << {name: 'Item 1'}

    item1 = store._items[0]

    item1_buffer = item1.buffer

    item1_buffer._name = 'Updated Item 1'
    item1_buffer._name
    # => 'Updated Item 1'

    item1._name
    # => 'Item 1'

    item1_buffer.save!

    item1_buffer._name
    # => 'Updated Item 1'

    item1._name
    # => 'Updated Item 1'
```

```#save!``` on buffer also returns a [promise](http://opalrb.org/blog/2014/05/07/promises-in-opal/) that will resolve when the data has been saved back to the server.

```ruby
    item1_buffer.save!.then do
      puts "Item 1 saved"
    end.fail do |err|
      puts "Unable to save because #{err}"
    end
```

Calling .buffer on an existing model will return a buffer for that model instance.  If you call .buffer on a Volt::ArrayModel (plural sub-collection), you will get a buffer for a new item in that collection.  Calling .save! will then add the item to that sub-collection as if you had done << to push the item into the collection.

---

## File: en/docs/callbacks_and_actions.md

# Controller Actions

When a template binding or tag renders a view, it first loads a controller.  There are four callbacks around the rendering.  The ```{action}``` is the same as the view file's name.  So if you were rendering ```about.html```, ```about``` would be the action.  Simply create the correct method in the controller and it will called at the time.

| action name           | description |
|-----------------------|-----------------------------------------------------|
| ```{action}``` | called before anything renders.  Setup data you need       |
| ```{action}_ready``` | called after the view renders.  Run any code where you need to bind directly to the dom.  jQuery setup code for example (bootstrap components) |
| ```before_{action}_remove``` | called before the view is removed (unrendered).  Cleanup any dom bindings here. |
| ```after_{action}_remove``` | called after the view is removed from the dom.  Cleanup anything in the controller that needs to be cleaned up. |

Most of the time all you will need is the action method to setup the controller, model, etc...

---

## File: en/docs/channel.md

# Channel

Controllers provide a `#channel` method, that you can use to get the status of the connection to the backend.  Channel's access methods are reactive and when the status changes, the watching computations will be re-triggered.  It provides the following:

| method      | description                                               |
|-------------|-----------------------------------------------------------|
| connected?  | true if it is connected to the backend                    |
| status      | possible values: :opening, :open, :closed, :reconnecting  |
| error       | the error message for the last failed connection          |
| retry_count | the number of reconnection attempts that have been made without a successful connection |
| reconnect_interval | the time until the next reconnection attempt (in seconds) |

---

## File: en/docs/component_generator.md

## Component Generator

Components can easily be shared as a gem.  Volt provides a scaffold command for creating component gems.  In a folder (not in a volt project), simply type: ```volt gem {component_name}```.  This will create the files needed for a new gem.  Note that all volt component gems will be prefixed with ```volt-``` so that they can easily be found by others on github and rubygems.

While developing, you can use the component by placing the following in your Gemfile:

```ruby
gem 'volt-{component_name}', path: '/path/to/folder/with/component'
```

Once the gem is ready, you can release it to rubygems with:

    rake release

Remove the ```path:``` option in the gemfile if you wish to use the rubygems version.

---

## File: en/docs/components.md

# Components

Apps are made up of Components.  Each folder under ```app/``` is a component.  When you visit a route, it loads all of the files in the component on the front end, so new pages within the component can be rendered without a new http request.  If a URL is visited that routes to a different component, the request will be loaded as a normal page load and all of that component's files will be loaded.  You can think of components as the "reload boundary" between sections of your app.

---

## File: en/docs/content_binding.md

# Content binding

The most basic binding is a content binding:

```html
<p>Hello {{ name }}</p>
```

The content binding runs the Ruby code between ```{{``` and ```}}```, then renders the return value.  Any time the data a content binding relies on changes, the binding will run again and update the text.  Text in content bindings is html escaped by default.