-
Notifications
You must be signed in to change notification settings - Fork 172
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
Showing
12 changed files
with
111 additions
and
59 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
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
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
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
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
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
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,45 @@ | ||
require "active_model/naming" | ||
|
||
module Trestle | ||
class ModelName | ||
attr_reader :klass | ||
|
||
delegate :downcase, :upcase, :titleize, :titlecase, to: :to_s | ||
|
||
def initialize(klass) | ||
@klass = klass | ||
|
||
if klass.respond_to?(:model_name) | ||
@name = klass.model_name | ||
else | ||
@name = ActiveModel::Name.new(klass) | ||
end | ||
end | ||
|
||
def ==(other) | ||
other.is_a?(self.class) && klass == other.klass | ||
end | ||
|
||
def to_s | ||
human | ||
end | ||
|
||
def singular(options={}) | ||
human(options) | ||
end | ||
alias_method :singularize, :singular | ||
|
||
def plural(options={}) | ||
if klass.respond_to?(:lookup_ancestors) && klass.respond_to?(:i18n_scope) | ||
human({ count: :many, default: human.pluralize }.merge(options)) | ||
else | ||
human.pluralize | ||
end | ||
end | ||
alias_method :pluralize, :plural | ||
|
||
def human(options={}) | ||
@name.human(options) | ||
end | ||
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
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
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,45 @@ | ||
require 'spec_helper' | ||
|
||
describe Trestle::ModelName do | ||
let(:klass) { stub_const("TestPost", Class.new(ActiveRecord::Base)) } | ||
subject(:name) { Trestle::ModelName.new(klass) } | ||
|
||
it "converts to string" do | ||
expect(name.to_s).to eq("Test post") | ||
end | ||
|
||
it "delegates string methods to to_s" do | ||
expect(name.downcase).to eq("test post") | ||
expect(name.upcase).to eq("TEST POST") | ||
expect(name.titleize).to eq("Test Post") | ||
expect(name.titlecase).to eq("Test Post") | ||
end | ||
|
||
it "has a singular form" do | ||
expect(name.singular).to eq("Test post") | ||
expect(name.singularize).to eq("Test post") | ||
expect(name.human).to eq("Test post") | ||
end | ||
|
||
it "has a plural form" do | ||
expect(name.plural).to eq("Test posts") | ||
expect(name.pluralize).to eq("Test posts") | ||
end | ||
|
||
it "is equal to another ModelName with the same class" do | ||
expect(name).to eq(Trestle::ModelName.new(klass)) | ||
expect(name).not_to eq(Trestle::ModelName.new(String)) | ||
end | ||
|
||
context "plain ruby class" do | ||
let(:klass) { stub_const("Article", Class.new) } | ||
|
||
it "has a singular form" do | ||
expect(name.singular).to eq("Article") | ||
end | ||
|
||
it "has a plural form" do | ||
expect(name.plural).to eq("Articles") | ||
end | ||
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
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