Skip to content

Commit

Permalink
Merge pull request #51 from elbartostrikesagain/humanizer_helper_class
Browse files Browse the repository at this point in the history
Adds a helper class as an alternative way to use the gem if users don…
  • Loading branch information
akonan authored Dec 19, 2024
2 parents d94d20f + 1dbe399 commit 31dc2f0
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 9 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3']

steps:
- uses: actions/checkout@v3

- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: Install dependencies
run: bundle install

- name: Run tests
run: bundle exec rspec
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Humanizer 2.7.1 (2024-12-19)

* Adds HumanizerHelper class for use cases when developers don't have a class they want to include Humanizer on(basic contact form for example)

### Humanizer 2.6.4 (2017-07-07)

* Prevents NoMethodError when form posts non-existing question id
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ end

4. If you are using strong_parameters, remember to permit `:humanizer_answer` and `:humanizer_question_id`.

## Usage without a model

Alternatively, you many use the built in HumanizerHelper class instead of using your own model (useful for something like a contact form if you don't have a model/class for this). Behavior is the same including `Humanizer` on a model, but all setters are available as optional arguments when initializing a HumanizerHelper instance.

1. Example initialization code(controller):

```ruby
@humanizer_helper = HumanizerHelper.new
```

2. Example rails form usage:

```erb
<%= label_tag :humanizer_answer, @humanizer_helper.humanizer_question %>
<%= text_field_tag :humanizer_answer %>
<%= hidden_field_tag :humanizer_question_id, @humanizer_helper.humanizer_question_id %>
```

3. Example response handling:

```ruby
humanizer_helper = HumanizerHelper.new(humanizer_answer: params[:humanizer_answer], humanizer_question_id: params[:humanizer_question_id])
if humanizer_helper.humanizer_correct_answer?
do_stuff
end
```

## Testing

A HumanizerHelper instance provides an additional `get_correct_humanizer_answer` method to make testing easier. Example:

```ruby
question_id = find('#humanizer_question_id', visible: false).value #gets humanizer question id from example form above
humanizer_helper = HumanizerHelper.new(humanizer_question_id: question_id)
fill_in 'humanizer_answer', with: humanizer_helper.get_correct_humanizer_answer #fills in answer field from example above with the correct answer
```


## Configuration

Default translations can be found in config/locales/
Expand Down Expand Up @@ -97,6 +135,7 @@ Humanizer is licensed under the MIT License, for more details see the LICENSE fi
* [seogrady](https://github.com/seogrady)
* [yairgo](https://github.com/yairgo)
* [woto](https://github.com/woto)
* [Calvin Delamere](https://github.com/elbartostrikesagain)

## CI Build Status

Expand Down
1 change: 1 addition & 0 deletions humanizer.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
require File.expand_path("../lib/humanizer/version", __FILE__)

Gem::Specification.new do |s|
Expand Down
14 changes: 6 additions & 8 deletions lib/humanizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ def humanizer_correct_answer?
def humanizer_questions
@humanizer_questions ||= begin
questions = I18n.translate!("humanizer.questions")
# Poor man's HashWithIndifferentAccess
# Create new mutable copies of the questions with indifferent access
questions.map do |question|
config = Hash.new
config.default_proc = proc do |h, k|
case k
when String then sym = k.to_sym; h[sym] if h.key?(sym)
when Symbol then str = k.to_s; h[str] if h.key?(str)
end
new_hash = {}
question.each do |k, v|
new_hash[k.to_s] = v # Store everything with string keys
new_hash[k.to_sym] = v # Store everything with symbol keys
end
config.merge(question)
new_hash
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/humanizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Humanizer
VERSION = "2.7.0"
VERSION = "2.7.1"
end
17 changes: 17 additions & 0 deletions lib/humanizer_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class HumanizerHelper
include ActiveModel::Validations
include Humanizer

def initialize(options={})
options[:humanizer_question_id] = options[:humanizer_question_id].to_i unless options[:humanizer_question_id].nil?
options.each do |k, v|
self.send("#{k}=", v)
end

humanizer_question_id
end

def get_correct_humanizer_answer
humanizer_answers_for_id(humanizer_question_id.to_i)[0]
end
end
38 changes: 38 additions & 0 deletions spec/humanizer_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'humanizer_helper'

describe HumanizerHelper do

context "initialized with variables" do
let(:incorrect_humanizer_answer) {"this is a wrong answer"}
let(:humanizer_question_id) {1}
let(:humanize_helper) {HumanizerHelper.new(humanizer_answer: incorrect_humanizer_answer, humanizer_question_id: humanizer_question_id.to_s)}

it "works as a placeholder class to humanizer using the variables passed in" do
expect(humanize_helper.humanizer_answer).to eq(incorrect_humanizer_answer)
expect(humanize_helper.humanizer_question_id).to eq(humanizer_question_id)
expect(humanize_helper.humanizer_correct_answer?).to be_falsey
expect(humanize_helper.humanizer_question).to eq('Jack and Jill went up the...')

expect(humanize_helper.get_correct_humanizer_answer).to eq('hill')
humanize_helper.humanizer_answer = "hill"
expect(humanize_helper.humanizer_correct_answer?).to be true

humanize_helper.change_humanizer_question(humanizer_question_id)
expect(humanize_helper.humanizer_question_id).to_not eq(humanizer_question_id)
end
end

context "initialized without variables" do
let(:humanize_helper) {HumanizerHelper.new}

it "works as a placeholder class to humanizer" do
expect(humanize_helper.humanizer_answer).to be_nil
expect(humanize_helper.humanizer_question_id).to be_present
expect(humanize_helper.humanizer_correct_answer?).to be_falsey

humanize_helper.humanizer_answer = humanize_helper.get_correct_humanizer_answer
expect(humanize_helper.humanizer_correct_answer?).to be true
end
end
end

0 comments on commit 31dc2f0

Please sign in to comment.