Skip to content

Commit

Permalink
❤️ More Readme ❤️
Browse files Browse the repository at this point in the history
  • Loading branch information
vjt committed Jun 5, 2014
1 parent 594b36b commit 9798ddd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
67 changes: 35 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,68 @@ Gemfile:

## Configuration

config/initializers/sanitizer.rb:
`config/initializers/sanitizer.rb`:

Sanitize::Rails.configure(
elements: [ ... ],
attribiutes: { ... },
elements: [ ... ],
attributes: { ... },
...
)

There's an example in the `example/` directory.

## Usage

app/models/foo.rb:
`app/models/foo.rb`:

sanitizes :field
sanitizes :some_other_field, on: :create
sanitizes :yet_another_field, on: :save
class Foo < ActiveRecord::Base
sanitizes :field
sanitizes :some_other_field, on: :create
sanitizes :yet_another_field, on: :save
end

ActionView `sanitize` helper is overriden to use
the Sanitize gem - transparently.
ActionView `sanitize` helper is transparently overriden to use the `Sanitize`
gem.

## Testing

### Test::Unit
### RSpec

test/test\_helper:
`spec/spec_helper.rb`:

require 'sanitize/rails/test_helpers'
require 'sanitize/rails/matchers'

Sanitize::Rails::TestHelpers.setup(self,
invalid: 'some <a>string',
valid: 'some <a>string</a>'
)
in spec code:

your test:
describe Post do
# Simplest variant, single field and default values
it { should sanitize_field :title }

assert_sanitizes(Model, :field, :some_other_field)
# Multiple fields
it { should sanitize_fields :title, :body }

### RSpec
# Specifing both text to sanitize and expected result
it { should sanitize_field(:title).replacing('&copy;').with('©') }
end

spec/spec\_helper:
You should pass field names to matcher in the same way as you do with the
`sanitize` call in the model, otherwise sanitize method won't be found in
model

require 'sanitize/rails/matchers'
### Test::Unit

in spec code:
`test/test_helper.rb:`

describe Post do
# Simplest variant, single field and default values
it { should sanitize_field :title }
require 'sanitize/rails/test_helpers'

# Multiple fields
it { should sanitize_fields :title, :body }
Sanitize::Rails::TestHelpers.setup(self,
invalid: 'some <a>string',
valid: 'some <a>string</a>'
)

# Specifing both text to sanitize and expected result
it { should sanitize_field(:title).replacing('&copy;').with('©') }
end
your test:

You should pass field names to matcher in the same way as you do with the `sanitize` call in the model, otherwise
sanitize method won't be found in model
assert_sanitizes Model, :field, :some_other_field

## Compatibility

Expand Down
8 changes: 8 additions & 0 deletions example/sanitizer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Sanitize::Rails example configuration
#
# https://github.com/vjt/sanitize-rails
#
# Enjoy, Share and Love <3
#
# -- [email protected]
#
HTML::WhiteListSanitizer.allowed_css_properties = %w(text-align background-color)
HTML::WhiteListSanitizer.shorthand_css_properties = %w()
HTML::WhiteListSanitizer.allowed_css_keywords = %w(left center right justify rgb)
Expand Down

0 comments on commit 9798ddd

Please sign in to comment.