contentful_rails 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 287f62b00e44548213d22bc2bd365a1f961324a6
4
- data.tar.gz: 280b863df836812bb18113e5ecab529f060c7007
3
+ metadata.gz: f0f7d55ee8546d4eed5140bb1768923d8bc6861b
4
+ data.tar.gz: c0616573406f91822b3d3ee04d5138625498497a
5
5
  SHA512:
6
- metadata.gz: 6d19254615f19e2d81439f30d165c4d78a444d8dfaa9c93e66652b9b6b4c9818656db82f43712a8440966d30ad602e0ca531e05a2939dc101f4124674ded03cb
7
- data.tar.gz: 40857c4ba6cb0565dab16183d8c57d27556a3d0ae6233cd5b2e50da6618466c46e99395767a4b3a6912ed86c13c2f3020a94f2fde7bb696884e1a149ea229cdf
6
+ metadata.gz: bae8ca92a8774946ed40da840af9815059925c9c0f386d0fe23cddb514d6b4ca3906822c07b003826a213b60bc41e7e27881062bba4efd963bed45319ea2629b
7
+ data.tar.gz: a934378c344537923460d5f96ee2328fe57ec49d316136eb4c6eaf5763fbed085574cccb121107bd929b1b1327b3550ecf7db5759ddf7d8035c9d0fdc52af1e1
data/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # Contentful Rails
2
+
3
+ A collection of useful things to help make it easier to integrate Contentful into your Rails app.
4
+ It includes view helpers, a Webhook handler, caching, and a Rails Engine to hook it all together.
5
+
6
+ This is a work in progress. It relies on the contentful_model gem (http://github.com/contentful/contentful_model)
7
+
8
+ ## What is Contentful?
9
+
10
+ [Contentful](https://www.contentful.com) is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.
11
+
12
+ # Configuration
13
+ ContentfulRails accepts a block for configuration. Best done in a Rails initializer.
14
+
15
+ ```
16
+ ContentfulRails.configure do |config|
17
+ config.authenticate_webhooks = true # false here would allow the webhooks to process without basic auth
18
+ config.webhooks_username = "a basic auth username"
19
+ config.webhooks_password = "a basic auth password"
20
+ config.access_token = "your access token"
21
+ config.preview_access_token = "your preview access token"
22
+ config.management_token = "your management access token"
23
+ config.space = "your space ID"
24
+ config.contentful_options = "hash of options"
25
+ end
26
+ ```
27
+
28
+ Note that you _don't_ have to separately configure ContentfulModel - adding the access tokens / space ID / options here will
29
+ pass to ContentfulModel in an initializer in the Rails engine.
30
+
31
+ The default is to authenticate the webhooks; probably a smart move to host on an HTTPS endpoint too.
32
+
33
+ ### Entry Mapping
34
+
35
+ By default, ContentfulRails will try to define your `entry_mapping` configuration for you. It does this by iterating through
36
+ the descendents of the base class `ContentfulModel::Base` during initialization. In order to ensure these classes are
37
+ loaded by this time, it will call `eager_load!` for the entire application. If this is not desired, you can set the
38
+ `eager_load_entry_mapping` config to false set your entry mapping manually by setting the entry_mapping config
39
+ as [described here](https://github.com/contentful/contentful.rb#custom-resource-classes).
40
+
41
+
42
+ ```
43
+ ContentfulRails.configure do |config|
44
+ ...
45
+ config.eager_load_entry_mapping = false
46
+ config.contentful_options = {
47
+ entry_mapping: {
48
+ 'article' => Article,
49
+ ...
50
+ }
51
+ }
52
+ end
53
+ ```
54
+
55
+ **Note:** If you do not define the entry mapping in your configuration, the webhook cache expiration will likely not work as expected
56
+
57
+ # Allowing 'Russian Doll' style caching on Entries
58
+ The issue with 'Russian Doll' caching in Rails is that it requires a hit on the database to check the `updated_at` timestamp of an object.
59
+
60
+ This is obviously expensive when the object is called over an API. So this gem wraps caches `updated_at` locally, and checks that first on subsequent calls.
61
+
62
+ ```
63
+ Foo.updated_at #returns a timestamp from cache, or from the API if no cache exists
64
+ ```
65
+
66
+ # Webhooks Endpoint
67
+ If there's a new version of an entry we need to expire the timestamp from the cache.
68
+
69
+ This gem includes a controller endpoint for Contentful to POST back to.
70
+
71
+ To make use of this in your app:
72
+
73
+ ## routes.rb
74
+ Mount the ContentfulRails engine at your preferred url:
75
+
76
+ ```
77
+ mount ContentfulRails::Engine => '/contentful' #feel free to choose a different endpoint name
78
+ ```
79
+
80
+ This will give you 2 routes:
81
+
82
+ `/contentful/webhooks` - the URL for contentful to post back to.
83
+ `/contentful/webhooks/debug` - a development-only URL to check you have mounted the engine properly :-)
84
+
85
+ ## What the webhook handler does
86
+ At the moment all this does is delete the timestamp cache entry, which means that a subsequent call to `updated_at` calls the API.
87
+
88
+ # View Helpers
89
+ Contentful has a [really nice url-based image manipulation API](https://www.contentful.com/blog/2014/08/14/do-more-with-images-on-contentful-platform/).
90
+
91
+ To take advantage of this, there's a custom Redcarpet renderer which allows you to pass the image parameters you want into the call to a `parse_markdown()` method.
92
+
93
+ In your application_controller.rb:
94
+
95
+ ```
96
+ helper ContentfulRails::MarkdownHelper
97
+ ```
98
+
99
+ This allows you to call `parse_markdown(@your_markdown)` and get HTML. *Note* that out of the box, the `parse_markdown()` is really permissive and allows you to put HTML in the Contentful markdown fields. This might not be what you want.
100
+
101
+ ## Manipulating images
102
+ To manipulate images which are referenced in your markdown, you can pass the following into the `parse_markdown()` call.
103
+
104
+ ```
105
+ parse_markdown(@your_markdown, image_options: {width: 1024, height: 1024})
106
+ ```
107
+
108
+ The `image_options` parameter takes the following options (some are mutually exclusive. Read the [instructions here](https://www.contentful.com/blog/2014/08/14/do-more-with-images-on-contentful-platform/)):
109
+
110
+ * `:width`
111
+ * `:height`
112
+ * `:fit`
113
+ * `:focus`
114
+ * `:corner_radius`
115
+ * `:quality`
116
+
117
+ ## Subclassing the MarkdownRenderer class
118
+ Sometimes you might want to apply some specific class, markup or similar to an html entity when it's being processed. [With RedCarpet that's dead easy](https://github.com/vmg/redcarpet#and-you-can-even-cook-your-own).
119
+
120
+ Just subclass the `ContentfulRails::MarkdownRenderer` class, and call any methods you need.
121
+
122
+ ```
123
+ class MyRenderer < ContentfulRails::MarkdownRenderer
124
+ # If you want to pass options into your renderer, you need to overload initialize()
125
+ def initialize(opts)
126
+ @options = opts
127
+ super
128
+ end
129
+
130
+ # If you want to do something special with links:
131
+ def link(link,title,content)
132
+ # Add a class name to all links, for example
133
+ class_name = "my-link-class-name"
134
+ content_tag(:a, content, href: link, title: title, class: class_name)
135
+ end
136
+ end
137
+ ```
138
+
139
+ You can overload any methods exposed in RedCarpet.
140
+
141
+ # To Do
142
+ Some things would be nice to do:
143
+
144
+ * Tests :-)
145
+ * Make caching the timestamp optional in the configuration
146
+ * Implement a method on ContentfulModel to simulate a parent-child relationship, so we can invalidate caches for parent items
147
+
148
+
149
+ # Licence
150
+ Licence is MIT. Please see MIT-LICENCE in this repo.
151
+
152
+ # Contributing
153
+ Please feel free to contribute!
154
+
155
+ * Fork this repo
156
+ * Make your changes
157
+ * Commit
158
+ * Create a PR
data/Rakefile CHANGED
@@ -6,27 +6,21 @@ end
6
6
 
7
7
  require 'rdoc/task'
8
8
 
9
+ require File.expand_path('../spec/dummy/config/application', __FILE__)
10
+
9
11
  RDoc::Task.new(:rdoc) do |rdoc|
10
12
  rdoc.rdoc_dir = 'rdoc'
11
13
  rdoc.title = 'ContentfulRails'
12
14
  rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
15
+ rdoc.rdoc_files.include('README.md')
14
16
  rdoc.rdoc_files.include('lib/**/*.rb')
15
17
  end
16
18
 
17
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
- load 'rails/tasks/engine.rake'
19
19
 
20
20
  Bundler::GemHelper.install_tasks
21
21
 
22
- require 'rake/testtask'
23
-
24
- Rake::TestTask.new(:test) do |t|
25
- t.libs << 'lib'
26
- t.libs << 'test'
27
- t.pattern = 'test/**/*_test.rb'
28
- t.verbose = false
29
- end
22
+ Rails.application.load_tasks
30
23
 
24
+ task(:default).clear
25
+ task default: :spec
31
26
 
32
- task default: :test
@@ -5,6 +5,7 @@ class ContentfulRails::WebhooksController < ActionController::Base
5
5
  http_basic_authenticate_with name: ContentfulRails.configuration.webhooks_username,
6
6
  password: ContentfulRails.configuration.webhooks_password
7
7
  end
8
+
8
9
  params = [:verify_authenticity_token, {:only => [:create], raise: false}]
9
10
  if Rails::VERSION::MAJOR > 4
10
11
  skip_before_action *params
@@ -3,6 +3,10 @@ module ContentfulRails
3
3
 
4
4
  isolate_namespace ContentfulRails
5
5
 
6
+ config.generators do |g|
7
+ g.test_framework :rspec
8
+ end
9
+
6
10
  config.before_initialize do
7
11
  if ContentfulRails.configuration.nil?
8
12
  ContentfulRails.configure {}
@@ -5,7 +5,9 @@ module ContentfulRails
5
5
  included do
6
6
  before_action :check_preview_domain
7
7
  after_action :remove_preview_cache
8
- helper_method :preview?
8
+ if respond_to?(:helper_method)
9
+ helper_method :preview?
10
+ end
9
11
  end
10
12
  # Check whether the subdomain being presented is the preview domain.
11
13
  # If so, set ContentfulModel to use the preview API, and request a username / password
@@ -1,3 +1,3 @@
1
1
  module ContentfulRails
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Error Creative Studio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: contentful_model
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.7
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.7
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
28
+ name: contentful_model
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 4.1.8
33
+ version: '0.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 4.1.8
40
+ version: '0.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: redcarpet
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - MIT-LICENSE
63
+ - README.md
63
64
  - Rakefile
64
65
  - app/controllers/contentful_rails/webhooks_controller.rb
65
66
  - app/helpers/contentful_rails/markdown_helper.rb
@@ -74,10 +75,6 @@ files:
74
75
  - lib/contentful_rails/sluggable.rb
75
76
  - lib/contentful_rails/version.rb
76
77
  - lib/tasks/contentful_rails_tasks.rake
77
- - test/contentful_rails_test.rb
78
- - test/dummy/Rakefile
79
- - test/integration/navigation_test.rb
80
- - test/test_helper.rb
81
78
  homepage: https://github.com/errorstudio/contentful_rails
82
79
  licenses:
83
80
  - MIT
@@ -102,8 +99,4 @@ rubygems_version: 2.5.1
102
99
  signing_key:
103
100
  specification_version: 4
104
101
  summary: A gem to help with hooking Contentful into your Rails application
105
- test_files:
106
- - test/contentful_rails_test.rb
107
- - test/dummy/Rakefile
108
- - test/integration/navigation_test.rb
109
- - test/test_helper.rb
102
+ test_files: []
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ContentfulRailsTest < ActiveSupport::TestCase
4
- test "truth" do
5
- assert_kind_of Module, ContentfulRails
6
- end
7
- end
data/test/dummy/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require 'rake'
2
- require 'rails'
@@ -1,10 +0,0 @@
1
- require 'test_helper'
2
-
3
- class NavigationTest < ActionDispatch::IntegrationTest
4
- fixtures :all
5
-
6
- # test "the truth" do
7
- # assert true
8
- # end
9
- end
10
-
data/test/test_helper.rb DELETED
@@ -1,16 +0,0 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
- ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
- require "rails/test_help"
7
-
8
- Rails.backtrace_cleaner.remove_silencers!
9
-
10
- # Load support files
11
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
12
-
13
- # Load fixtures from the engine
14
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
15
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
16
- end