Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new password field #998

Merged
merged 1 commit into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/views/fields/password/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%#
# Password Form Partial

This partial renders an input element for a password attribute.
By default, the input is a password field.

## Local variables:

- `f`:
A Rails form generator, used to help create the appropriate input fields.
- `field`:
An instance of [Administrate::Field::Password][1].
A wrapper around the Password.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Password
%>

<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.password_field field.attribute, value: field.data %>
</div>
18 changes: 18 additions & 0 deletions app/views/fields/password/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%#
# Password Index Partial

This partial renders a password attribute
to be displayed on a resource's index page.

By default, the attribute is rendered as a truncated string.

## Local variables:

- `field`:
An instance of [Administrate::Field::Password][1].
A wrapper around the Password.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Password
%>

<%= field.truncate %>
18 changes: 18 additions & 0 deletions app/views/fields/password/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%#
# Password Show Partial

This partial renders a password attribute,
to be displayed on a resource's show page.

By default, the attribute is rendered as an truncate string.

## Local variables:

- `field`:
An instance of [Administrate::Field::Password][1].
A wrapper around the Password.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Password
%>

<%= field.truncate %>
12 changes: 12 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ specify, including:
- `Field::Select`
- `Field::String`
- `Field::Text`
- `Field::Password`

## Customizing Fields

Expand Down Expand Up @@ -158,6 +159,17 @@ Default is `false`.
`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

**Field::Password**

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

`:truncate` - Set the number of characters to display in the views.
Defaults to `50`.

`:character` - Set the replace character.
Defaults to `•`.

### Defining Labels

To change the user-facing label for an attribute,
Expand Down
1 change: 1 addition & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "administrate/field/string"
require "administrate/field/text"
require "administrate/field/time"
require "administrate/field/password"

module Administrate
class BaseDashboard
Expand Down
25 changes: 25 additions & 0 deletions lib/administrate/field/password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative "base"

module Administrate
module Field
class Password < Field::Base
def self.searchable?
false
end

def truncate
data.to_s.gsub(/./, character)[0...truncation_length]
end

private

def truncation_length
options.fetch(:truncate, 50)
end

def character
options.fetch(:character, "•")
end
end
end
end
4 changes: 3 additions & 1 deletion spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ class CustomerDashboard < Administrate::BaseDashboard
kind: Field::Select.with_options(collection: Customer::KINDS),
country: Field::BelongsTo.
with_options(primary_key: :code, foreign_key: :country_code),
password: Field::Password,
}

COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys
COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys - %i[created_at updated_at]
SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys - [:name]
FORM_ATTRIBUTES = [
:name,
:email,
:email_subscriber,
:kind,
:country,
:password,
].freeze

def display_resource(customer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPasswordToCustomers < ActiveRecord::Migration[4.2]
def change
add_column :customers, :password, :string
end
end
1 change: 1 addition & 0 deletions spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
t.boolean "email_subscriber"
t.string "kind", default: "standard", null: false
t.string "country_code"
t.string "password"
end

create_table "delayed_jobs", id: :serial, force: :cascade do |t|
Expand Down
1 change: 1 addition & 0 deletions spec/example_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
name: name,
email: Faker::Internet.safe_email(name),
country: countries.sample,
password: Faker::Internet.password,
}
end

Expand Down
65 changes: 65 additions & 0 deletions spec/lib/fields/password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "administrate/field/password"
require "support/field_matchers"

describe Administrate::Field::Password do
include FieldMatchers

describe "#to_partial_path" do
it "returns a partial based on the page being rendered" do
page = :show
field = Administrate::Field::Password.new(:password, "my_password", page)

path = field.to_partial_path

expect(path).to eq("/fields/password/#{page}")
end
end

it { should_permit_param(:foo, for_attribute: :foo) }

describe "#truncate" do
it "renders an empty string for nil" do
string = Administrate::Field::Password.new(:secret, nil, :show)

expect(string.truncate).to eq("")
end

it "defaults to displaying up to 50 characters" do
short = Administrate::Field::Password.new(:short_secret, lorem(30), :show)
long = Administrate::Field::Password.new(:long_secret, lorem(60), :show)

expect(short.truncate).to eq(lorem(30))
expect(long.truncate).to eq(lorem(50))
end

context "with a `truncate` option" do
it "shortens to the given length" do
password = password_with_options(lorem(30), truncate: 20)

expect(password.truncate).to eq(lorem(20))
end

it "different to default character" do
password = password_with_options(lorem(30), character: "*")

expect(password.truncate).to eq(lorem(30, "*"))
end

it "shortens to the given length & different to default character" do
password = password_with_options(lorem(30),
truncate: 10,
character: "-")

expect(password.truncate).to eq(lorem(10, "-"))
end
end
end

def password_with_options(string, options)
Administrate::Field::Password.new(:string, string, :page, options)
end

def lorem(n, character = "•")
character * n
end
end