Skip to content

Commit

Permalink
Set custom block types (#57)
Browse files Browse the repository at this point in the history
* Speed up block detection by eliminating NameError were possible

* Add readme for 0.6

* Tabs for spaces in changelog
  • Loading branch information
raffij authored Feb 19, 2019
1 parent 8e19c00 commit 931b955
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.6.0:
date: 2019-02-19
- Added custom_block_types to improve block class lookup performance

v0.4.0:
date: 2014-05-20
changes:
Expand Down
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Sir Trevor Rails

A Rails gem for integrating Sir Trevor JS into your Rails 3/4 application.
A Rails gem for integrating Sir Trevor JS into your Rails 3/4/5 application.

## Upgrade guide to v0.6.0

There is a breaking change which needs to be applied if you've defined custom ruby blocks.

Create an initializer ``config/initializers/sir_trevor_rails.rb``

Add your custom block types in the initializer:

```ruby
class SirTrevorRails::Block
def self.custom_block_types
# Type should be string based and prefix of your class name.
["Custom"] # Would relate to CustomBlock
end
end
```

## Upgrade guide from v0.4.0-rc.1

Expand Down Expand Up @@ -74,6 +91,22 @@ Does this content have an image block?
Return the first video block in the content
## Add custom block types with custom methods
Create an initializer ``config/initializers/sir_trevor_rails.rb``
Add your custom block types in the initializer:
```ruby
class SirTrevorRails::Block
def self.custom_block_types
# Type should be string based and prefix of your class name which would be for the following.
# TextBlock, HeadingBlock, CustomBlock
["Text", "Heading", "Custom"]
end
end
```
## Add custom methods for block content
Create an initializer ``config/initializers/sir_trevor_rails.rb``
Expand Down
18 changes: 14 additions & 4 deletions lib/sir_trevor_rails/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def format
send(:[], :format).present? ? send(:[], :format).to_sym : DEFAULT_FORMAT
end

# Sets a list of custom block types to speed up lookup at runtime.
def self.custom_block_types
[]
end

def initialize(hash, parent)
@raw_data = hash
@parent = parent
Expand Down Expand Up @@ -40,10 +45,15 @@ def as_json(*attrs)
#
# @param [Symbol] type
def self.block_class(type)
block_name = "#{type.to_s.camelize}Block"
begin
block_name.constantize
rescue NameError
type_name = type.to_s.camelize
block_name = "#{type_name}Block"
if custom_block_types.include?(type_name)
begin
block_name.constantize
rescue NameError
block_class!(block_name)
end
else
block_class!(block_name)
end
end
Expand Down
2 changes: 1 addition & 1 deletion sir_trevor_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "wrong"
spec.add_development_dependency "pry-rails"
spec.add_development_dependency "combustion"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "sqlite3", "~> 1.3.6"
spec.add_development_dependency "capybara"
spec.add_development_dependency "launchy"

Expand Down

0 comments on commit 931b955

Please sign in to comment.