-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 580d13d
Showing
99 changed files
with
2,604 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
name: CI | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
rspec: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
ruby: | ||
- '3.1' | ||
- '3.0' | ||
- '2.7' | ||
rails: | ||
- rails_6.0.4 | ||
- rails_6.1.5 | ||
- rails_7.0.2 | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
|
||
- name: Setup Ruby cache | ||
uses: actions/cache@v2 | ||
with: | ||
path: vendor/bundle | ||
key: ${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.rails }}-${{ hashFiles('**/Gemfile.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.rails }}- | ||
- name: Bundle | ||
env: | ||
RAILS_VERSION: ${{ matrix.rails }} | ||
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile | ||
run: | | ||
gem install bundler | ||
bundle config path vendor/bundle | ||
bundle install --jobs 4 --retry 3 | ||
- name: RSpec | ||
env: | ||
RAILS_VERSION: ${{ matrix.rails }} | ||
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile | ||
run: | | ||
bin/rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Ignore bundler config. | ||
.bundle/ | ||
|
||
# Ignore Gemfile.lock | ||
Gemfile.lock | ||
gemfiles/*.lock | ||
|
||
# Ignore test files | ||
coverage/ | ||
|
||
# Ignore dummy app files | ||
spec/dummy/db/*.sqlite3 | ||
spec/dummy/db/*.sqlite3-journal | ||
spec/dummy/log/*.log | ||
spec/dummy/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
AllCops: | ||
NewCops: enable | ||
SuggestExtensions: false | ||
TargetRubyVersion: 2.7 | ||
Exclude: | ||
- bin/* | ||
- gemfiles/* | ||
- spec/**/* | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Style/StringLiterals: | ||
EnforcedStyle: double_quotes | ||
|
||
Layout/EmptyLinesAroundModuleBody: | ||
Enabled: false | ||
|
||
Layout/EmptyLinesAroundClassBody: | ||
Enabled: false | ||
|
||
Layout/IndentationConsistency: | ||
EnforcedStyle: indented_internal_methods | ||
|
||
Layout/LineLength: | ||
Max: 140 | ||
|
||
Metrics/CyclomaticComplexity: | ||
Max: 8 | ||
|
||
Gemspec/RequireMFA: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RAILS_VERSIONS = %w[ | ||
6.0.4 | ||
6.1.5 | ||
7.0.2 | ||
].freeze | ||
|
||
RAILS_VERSIONS.each do |version| | ||
appraise "rails_#{version}" do | ||
gem "rails", version | ||
gem "sprockets-rails" if ["7.0.2"].include?(version) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in devise-otp.gemspec | ||
gemspec | ||
gem "sprockets-rails" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
guard :rspec, cmd: "bundle exec rspec" do | ||
require "guard/rspec/dsl" | ||
dsl = Guard::RSpec::Dsl.new(self) | ||
|
||
# RSpec files | ||
rspec = dsl.rspec | ||
watch(rspec.spec_helper) { rspec.spec_dir } | ||
watch(rspec.spec_support) { rspec.spec_dir } | ||
watch(rspec.spec_files) | ||
|
||
# Ruby files | ||
ruby = dsl.ruby | ||
dsl.watch_spec_files_for(ruby.lib_files) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# Draper | ||
|
||
[![GitHub license](https://img.shields.io/github/license/jbox-web/draper.svg)](https://github.com/jbox-web/draper/blob/master/LICENSE) | ||
[![GitHub release](https://img.shields.io/github/release/jbox-web/draper.svg)](https://github.com/jbox-web/draper/releases/latest) | ||
[![CI](https://github.com/jbox-web/draper/workflows/CI/badge.svg)](https://github.com/jbox-web/draper/actions) | ||
[![Code Climate](https://codeclimate.com/github/jbox-web/draper/badges/gpa.svg)](https://codeclimate.com/github/jbox-web/draper) | ||
[![Test Coverage](https://codeclimate.com/github/jbox-web/draper/badges/coverage.svg)](https://codeclimate.com/github/jbox-web/draper/coverage) | ||
|
||
Draper adds an object-oriented layer of presentation logic to your Rails | ||
application. | ||
|
||
Without Draper, this functionality might have been tangled up in procedural | ||
helpers or adding bulk to your models. With Draper decorators, you can wrap your | ||
models with presentation-related logic to organise - and test - this layer of | ||
your app much more effectively. | ||
|
||
## Installation | ||
|
||
Put this in your `Gemfile` : | ||
|
||
```ruby | ||
git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" } | ||
|
||
gem 'draper', github: 'jbox-web/draper', tag: '1.0.0' | ||
``` | ||
|
||
then run `bundle install`. | ||
|
||
## Writing Decorators | ||
|
||
Decorators inherit from `Draper::Decorator`, live in your `app/decorators` | ||
directory, and are named for the model that they decorate: | ||
|
||
```ruby | ||
# app/decorators/article_decorator.rb | ||
class ArticleDecorator < Draper::Decorator | ||
# ... | ||
end | ||
``` | ||
|
||
### Accessing Helpers | ||
|
||
Normal Rails helpers are still useful for lots of tasks. Both Rails' provided | ||
helpers and those defined in your app can be accessed within a decorator via the `h` method: | ||
|
||
```ruby | ||
class ArticleDecorator < Draper::Decorator | ||
def emphatic | ||
h.content_tag(:strong, "Awesome") | ||
end | ||
end | ||
``` | ||
|
||
### Accessing the model | ||
|
||
When writing decorator methods you'll usually need to access the wrapped model. | ||
While you may choose to use delegation ([covered below](#delegating-methods)) | ||
for convenience, you can always use the `object` (or its alias `model`): | ||
|
||
```ruby | ||
class ArticleDecorator < Draper::Decorator | ||
def published_at | ||
object.published_at.strftime("%A, %B %e") | ||
end | ||
end | ||
``` | ||
|
||
### Decorating Associated Objects | ||
|
||
You can automatically decorate associated models when the primary model is | ||
decorated. Assuming an `Article` model has an associated `Author` object: | ||
|
||
```ruby | ||
class ArticleDecorator < Draper::Decorator | ||
decorates_association :author | ||
end | ||
``` | ||
|
||
When `ArticleDecorator` decorates an `Article`, it will also use | ||
`AuthorDecorator` to decorate the associated `Author`. | ||
|
||
### Delegating Methods | ||
|
||
When your decorator calls `delegate_all`, any method called on the decorator not | ||
defined in the decorator itself will be delegated to the decorated object. This | ||
includes calling `super` from within the decorator. A call to `super` from within | ||
the decorator will first try to call the method on the parent decorator class. If | ||
the method does not exist on the parent decorator class, it will then try to call | ||
the method on the decorated `object`. This is a very permissive interface. | ||
|
||
If you want to strictly control which methods are called within views, you can | ||
choose to only delegate certain methods from the decorator to the source model: | ||
|
||
```ruby | ||
class ArticleDecorator < Draper::Decorator | ||
delegate :title, :body | ||
end | ||
``` | ||
|
||
We omit the `:to` argument here as it defaults to the `object` being decorated. | ||
You could choose to delegate methods to other places like this: | ||
|
||
```ruby | ||
class ArticleDecorator < Draper::Decorator | ||
delegate :title, :body | ||
delegate :name, :title, to: :author, prefix: true | ||
end | ||
``` | ||
|
||
From your view template, assuming `@article` is decorated, you could do any of | ||
the following: | ||
|
||
```ruby | ||
@article.title # Returns the article's `.title` | ||
@article.body # Returns the article's `.body` | ||
@article.author_name # Returns the article's `author.name` | ||
@article.author_title # Returns the article's `author.title` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application '_guard-core' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require "pathname" | ||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", | ||
Pathname.new(__FILE__).realpath) | ||
|
||
bundle_binstub = File.expand_path("../bundle", __FILE__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("guard", "_guard-core") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'appraisal' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require "pathname" | ||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", | ||
Pathname.new(__FILE__).realpath) | ||
|
||
bundle_binstub = File.expand_path("../bundle", __FILE__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("appraisal", "appraisal") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'guard' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require "pathname" | ||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", | ||
Pathname.new(__FILE__).realpath) | ||
|
||
bundle_binstub = File.expand_path("../bundle", __FILE__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("guard", "guard") |
Oops, something went wrong.