Skip to content

Commit

Permalink
Merge pull request #37 from goosys/wip/issue-6
Browse files Browse the repository at this point in the history
Wip/issue 6
  • Loading branch information
goosys authored Sep 3, 2024
2 parents 8a081ee + 549cae9 commit f5820d3
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gemspec
gem "rake", "~> 13.0"

gem "enumerize"
gem 'rails-i18n'
gem "ransack"
gem 'refile', require: 'refile/rails', github: 'vitalinfo/refile'
gem 'refile-mini_magick'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Add the following to your Gemfile.
```ruby
gem "sassc-rails"
gem "enumerize"
gem 'rails-i18n'
gem "refile", require: "refile/rails", github: "vitalinfo/refile", branch: "latest_ruby_rails"
gem "refile-mini_magick"

Expand Down
13 changes: 12 additions & 1 deletion app/views/fields/has_many/_index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
<%= t('administrate.fields.has_many.pluralize', count: field.data.size) %>
<%=
t(
"administrate.fields.has_many.pluralize.attributes.#{field.resource.class.model_name.i18n_key}/#{field.attribute}",
default: [
:"administrate.fields.has_many.pluralize.models.#{field.associated_class.model_name.i18n_key}",
:'administrate.fields.has_many.pluralize'
],
count: ( count = field.data.size ),
pluralize: pluralize(count, ( human = field.associated_class.model_name.human(default: field.associated_class.model_name.human.titleize))),
model_name: human.pluralize(count, I18n.locale)
)
%>
13 changes: 12 additions & 1 deletion app/views/fields/nested_has_many/_index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
<%= t('administrate.fields.has_many.pluralize', count: field.data.size) %>
<%=
t(
"administrate.fields.has_many.pluralize.attributes.#{field.resource.class.model_name.i18n_key}/#{field.attribute}",
default: [
:"administrate.fields.has_many.pluralize.models.#{field.associated_class.model_name.i18n_key}",
:'administrate.fields.has_many.pluralize'
],
count: ( count = field.data.size ),
pluralize: pluralize(count, ( human = field.associated_class.model_name.human(default: field.associated_class.model_name.human.titleize))),
model_name: human.pluralize(count)
)
%>
8 changes: 8 additions & 0 deletions config/locales/administrate.en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
en:
administrate:
# fields:
# has_many:
# pluralize:
# zero: ""
# one: "%{pluralize}"
# other: "%{pluralize}"
# # other: "%{count} %{model_name}"
# # other: "%{count} Items"
application:
home:
title: Dashboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

expect(rendered).to match('id="new_book')
expect(rendered).to match('action="/admin/books/new"')
ensure
remove_constants :Book
end
end

Expand All @@ -43,6 +45,8 @@

expect(rendered).to match('id="new_book')
expect(rendered).to match('action="/admin/books/new"')
ensure
remove_constants :Book
end
end

Expand All @@ -68,6 +72,8 @@

expect(rendered).to match('id="new_image')
expect(rendered).to match('action="/admin/books/1/images/new"')
ensure
remove_constants :Book, :Image
end
end
end
Expand Down
280 changes: 280 additions & 0 deletions spec/administrate/views/fields/has_many/_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
require "rails_helper"

describe "fields/has_many/_index", type: :view do
before do
view.lookup_context.prepend_view_paths [File.expand_path("../../../../../app/views", __dir__)] #TODO
end

let(:field_default_formats_en) do
YAML.safe_load(%(
administrate:
fields:
has_many:
pluralize:
zero: "%{pluralize}"
one: "%{pluralize}"
other: "%{pluralize}"
))
end

context "If locale is :en (plural keys is [:zero, :one, :other])" do
around(:each) do |example|
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations(:en, formats)
I18n.with_locale(:en) do
example.run
end
end

before do
class Book < ApplicationRecord; self.abstract_class = true; end
class BookImage < ApplicationRecord; self.abstract_class = true; end

allow(view).to receive_message_chain("field.resource.class").and_return(Book)
allow(view).to receive_message_chain("field.attribute").and_return("book_images")
allow(view).to receive_message_chain("field.associated_class").and_return(BookImage)
end

after do
remove_constants :Book, :BookImage
end

context "If there is no translation for pluralize and no translation for the Model" do
let(:formats) { field_default_formats_en }

context "If the target model has no namespace" do
it "displays the pluralized and titleized model name" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 Book Images")
end
end

context "If the target model has a namespace" do
it "displays the pluralized and titleized model name" do
module Library
class Book < ApplicationRecord; self.abstract_class = true; end
class BookImage < ApplicationRecord; self.abstract_class = true; end
end

allow(view).to receive_message_chain("field.resource.class").and_return(Library::Book)
allow(view).to receive_message_chain("field.attribute").and_return("book_images")
allow(view).to receive_message_chain("field.associated_class").and_return(Library::BookImage)
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 Book Images")
ensure
remove_constants :Library
end
end
end

context "If there are translations for the Model" do
let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "Special Book"
book_image: "Special Book Image"
)).deep_merge(field_default_formats_en)
end

context "If the target model has no namespace" do
it "displays the pluralized and titleized model name with translation" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 Special Book Images")
end
end
end

context "If there are translations for the Model" do
let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "Special Book"
book_image: "Special Book Image"
administrate:
fields:
has_many:
pluralize:
zero: "%{pluralize}"
one: "%{pluralize}"
other: "%{pluralize}"
attributes:
"book/book_images":
zero: "%{pluralize}"
one: "%{pluralize}"
other: "Current %{pluralize}"
))
end

context "If the target model has no namespace" do
it "displays the pluralized and titleized model name with translation from administrate.fields.has_many.pluralize.attributes.PARENT_CLASS_NAME.ATTRIBUTE_NAME" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("Current 2 Special Book Images")
end
end
end
end

context "If locale is :ja (plural keys only is [:other])" do
around(:each) do |example|
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations(:ja, formats)
I18n.with_locale(:ja) do
example.run
end
end

before do
class Book < ApplicationRecord; self.abstract_class = true; end
class BookImage < ApplicationRecord; self.abstract_class = true; end

allow(view).to receive_message_chain("field.resource.class").and_return(Book)
allow(view).to receive_message_chain("field.attribute").and_return("book_images")
allow(view).to receive_message_chain("field.associated_class").and_return(BookImage)
end

after do
remove_constants :Book, :BookImage
end

context "When using a common unit regardless of the model name" do
let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "書籍"
book_image: "書籍画像"
administrate:
fields:
has_many:
pluralize:
zero: ""
other: "%{count} 件"
))
end

it "displays item counts" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 件")
end

context "When translating with specific attributes" do
let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "書籍"
book_image: "書籍画像"
administrate:
fields:
has_many:
pluralize:
zero: ""
other: "%{count} 件"
attributes:
book/book_images:
zero: ""
other: "%{count} 枚"
))
end

it "displays item counts with specific translation" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 枚")
end
end
end

context "If the model_name option is used" do
let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "書籍"
book_image: "書籍画像"
administrate:
fields:
has_many:
pluralize:
zero: ""
other: "%{count} %{model_name}"
))
end

it "displays item counts with model_name (since it's in Japanese, no pluralization)" do
allow(view).to receive_message_chain("field.data.size").and_return(2)

render
expect(rendered.chomp).to eq("2 書籍画像")
end
end
end

context "If locale is :ru (plural keys is [:one, :few, :many, :other])" do
around(:each) do |example|
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations(:ru, formats)
I18n.with_locale(:ru) do
example.run
end
end

before do
class Book < ApplicationRecord; self.abstract_class = true; end
class BookImage < ApplicationRecord; self.abstract_class = true; end

allow(view).to receive_message_chain("field.resource.class").and_return(Book)
allow(view).to receive_message_chain("field.attribute").and_return("book_images")
allow(view).to receive_message_chain("field.associated_class").and_return(BookImage)
end

after do
remove_constants :Book, :BookImage
end

let(:formats) do
YAML.safe_load(%(
activerecord:
models:
book: "книга"
book_image: "изображение книги"
administrate:
fields:
has_many:
pluralize:
zero: "no items"
one: "one item"
few: "few items"
many: "many items"
other: "other items"
))
end

it "uses the translation matching 'few'" do
allow(view).to receive_message_chain("field.data.size").and_return(3)

render
expect(rendered.chomp).to eq("few items")
end

it "uses the translation matching 'many'" do
allow(view).to receive_message_chain("field.data.size").and_return(11)

render
expect(rendered.chomp).to eq("many items")
end
end
end

0 comments on commit f5820d3

Please sign in to comment.