Skip to content

Commit

Permalink
Merge branch 'master' into 0.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 3, 2015
2 parents fdaf098 + c390709 commit f17df49
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Lotus::Helpers
View helpers for Ruby applications

## v0.2.0 - 2015-06-23
### Added
- [Luca Guidi] Introduced `Lotus::Helpers::FormHelper`. HTML5 form generator (`#form_for`).
- [Tom Kadwill & Luca Guidi] Introduced `Lotus::Helpers::NumberFormattingHelper`. Format numbers (`#format_number`).

## v0.1.0 - 2015-03-23
### Added
- [Luca Guidi] Introduced `Lotus::Helpers::RoutingHelper`. It exposes `#routes` in views for compatibility with Lotus (`lotusrb` gem)
Expand Down
168 changes: 167 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,142 @@ Output:
</aside>
```

### Form Helper

Form generator for HTML5 (`#form_for`)

#### Template Usage

Template:

```erb
<%=
form_for :book, routes.books_path do
text_field :title
submit 'Create'
end
%>
```

Output:

```html
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
```

#### View Usage

View:

```ruby
module Books
class New
include Lotus::Helpers

def form
form_for :book, routes.books_path do
text_field :title

submit 'Create'
end
end
end
end
```

Template:

```erb
<%= form %>
```

Output:

```html
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
```

#### Reuse Code

Views:

```ruby
module Books
class New
include Lotus::Helpers

def form
Form.new(:book, routes.books_path)
end

def submit_label
'Create'
end
end

class Edit
include Lotus::Helpers

def form
Form.new(:book, routes.book_path(id: book.id), {book: book}, {method: :patch})
end

def submit_label
'Update'
end
end
end
```

Templates:

```erb
# books/new.html.erb
<%= render partial: 'books/form' %>
```

```erb
# books/edit.html.erb
<%= render partial: 'books/form' %>
```

```erb
# books/_form.html.erb
<%=
form_for form, class: 'form-horizontal' do
text_field :title
submit submit_label
end
%>
```

Output for new:

```html
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
```

Output for edit:

```html
<form action="/books/23" method="POST" accept-charset="utf-8" id="book-form">
<input type="hidden" name="_method" value="PATCH">
<input type="text" name="book[title]" id="book-id" value="TDD">
<button type="submit">Update</button>
</form>
```

### Escape helper

HTML (`#h`), HTML attribute (`#ha`) and URL (`#hu`) escape helpers.
Expand Down Expand Up @@ -128,7 +264,7 @@ Output:
<code>puts "Hello, World!"</code>
```

### Routing helper
### Routing Helper

Lotus and Lotus::Router integration (`#routes`).

Expand Down Expand Up @@ -158,6 +294,36 @@ Output:
<a href="/">Home</a>
```

### Number Formatting Helper

Format numbers (`#format_number`).

View:

```ruby
module Home
class Index
include Lotus::Helpers

def visitors_count
format_number '1000'
end
end
end
```

Template:

```erb
<p><%= visitors_count %></p>
```

Output:

```html
<p>1,000</p>
```

## Philosophy

All the Lotus helpers are modules to include.
Expand Down
2 changes: 2 additions & 0 deletions lib/lotus/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'lotus/helpers/routing_helper'
require 'lotus/helpers/link_to_helper'
require 'lotus/helpers/form_helper'
require 'lotus/helpers/number_formatting_helper'

module Lotus
# View helpers for Ruby applications
Expand All @@ -25,6 +26,7 @@ def self.included(base)
include Lotus::Helpers::RoutingHelper
include Lotus::Helpers::LinkToHelper
include Lotus::Helpers::FormHelper
include Lotus::Helpers::NumberFormattingHelper
end
end
end
Expand Down

0 comments on commit f17df49

Please sign in to comment.