cadmus 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: baa8f826659300b741b1fac079ee71bb74315514
4
- data.tar.gz: 1422e3236c83edcc178b76867f568e4cd4122149
3
+ metadata.gz: 215a6a345f843d05ef56b703311301119d8bd685
4
+ data.tar.gz: 171b52fb98dcd2779c8fa28f8128a8705aea3411
5
5
  SHA512:
6
- metadata.gz: e8e8447ace4df8e75b8910fb9d2d6f5e50b39200fd91a264f497122326aa38c66c3c07a827085f5b0431399a45564e35a5723c1822db1f35b718141f44d34a95
7
- data.tar.gz: 47f2c14c40f57c03a5b4b22284245c6f3b69c37a067fc2c78cbaaecc35aa1f47fc9ac7b2f81f8dc5ad8de383bcc98af1df33bfc1e7101deca9dc62f469385ac1
6
+ metadata.gz: 138d7e20134104dd9b7f1252def6317678df7038b0ff920569673878437f72f4aeffeed5abd7c980e017ecae568f72b217ea0cb13b4b4d26bb3e4f2d79f56bbe
7
+ data.tar.gz: 17f27f48c748c06284811f8b3e7cdcbe549986a97b2a4214146b2e5b24e6ba0e54f4edafa5970ee8c229c3e89f8cf06c942f56a755833f5e22f2b382187f3b3b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## cadmus 0.6.0 (04-08-2017)
2
+
3
+ * Expose a `{% page_url %}` tag, which outputs the URL to a page (for easier inter-page linking)
4
+ * Expose a Liquid register called `parent` from controllers that mix in `Cadmus::ControllerExtensions`, which contains the page parent (if present)
5
+ * Extract a mixin module called `Cadmus::LiquidTemplateField`, which makes it easier to add Liquid templates to models
6
+
1
7
  ## cadmus 0.5.3 (02-04-2017)
2
8
 
3
9
  * Make it possible to make `liquid_assigns`, `liquid_filters` and `liquid_registers` private or protected methods in controllers and other Renderables.
data/README.md CHANGED
@@ -14,7 +14,7 @@ ORM you want instead of ActiveRecord.
14
14
 
15
15
  One additional feature is the ability for pages to have parents. A parent can be any model object. Page parent objects allow
16
16
  you to create separate "sections" of your site - for example, if you have a project-hosting application that includes multiple
17
- projects, each of which has its own separate space of CMS pages. (Page parents aren't intended for creating sub-pages -
17
+ projects, each of which has its own separate space of CMS pages. (Page parents aren't intended for creating sub-pages -
18
18
  instead, just use forward-slash characters in the page slug to simulate folders, and Cadmus will handle it.)
19
19
 
20
20
  ## Basic Installation
@@ -30,14 +30,14 @@ The next step is to create a Page model. Your app can have multiple Page models
30
30
  create one.
31
31
 
32
32
  rails generate model Page name:text slug:string content:text parent_id:integer parent_type:string
33
-
34
- You'll need to tweak the generated migration and model slightly. In the migration, after the `create_pages` block, add a
33
+
34
+ You'll need to tweak the generated migration and model slightly. In the migration, after the `create_pages` block, add a
35
35
  unique index on the parent and slug columns:
36
36
 
37
37
  ```ruby
38
38
  add_index :pages, [:parent_type, :parent_id, :slug], :unique => true
39
39
  ```
40
-
40
+
41
41
  And in the model, add a `cadmus_page` declaration:
42
42
 
43
43
  ```ruby
@@ -77,7 +77,7 @@ class PagesController < ApplicationController
77
77
  def page_params
78
78
  params.require(:page).permit(:name, :slug, :content)
79
79
  end
80
-
80
+
81
81
  def page_class
82
82
  Page
83
83
  end
@@ -89,6 +89,16 @@ define a `page_class` method that returns the class for pages it's dealing with.
89
89
  classes depending on request parameters, if you need it to - or, you could also set up different controllers for different
90
90
  types of page.)
91
91
 
92
+ You'll also want to set up an initializer (probably in `config/initializers/cadmus.rb`) that will tell Cadmus how to generate URLs for pages. This might look like this:
93
+
94
+ ```ruby
95
+ Cadmus::Tags::PageUrl.define_page_path_method do |page_name, parent|
96
+ page_path(page_name, parent_id: parent)
97
+ end
98
+ ```
99
+
100
+ Doing this will enable markup like this in page templates: `<a href="{% page_url my-other-page %}">My other page</a>`. The `{% page_url my-other-page %}` will be replaced with the actual URL for the page called `my-other-page`.
101
+
92
102
  Finally, you'll need to create routes for this controller. Cadmus provides a built-in helper for that:
93
103
 
94
104
  ```ruby
@@ -96,7 +106,7 @@ MyApp::Application.routes.draw do
96
106
  cadmus_pages
97
107
  end
98
108
  ```
99
-
109
+
100
110
  This will create the following routes:
101
111
 
102
112
  * GET /pages => PagesController#index
@@ -109,15 +119,15 @@ This will create the following routes:
109
119
 
110
120
  ## Authorization Control
111
121
 
112
- The pages controller is where you'll need to hook into any authorization or authentication system your app might use.
122
+ The pages controller is where you'll need to hook into any authorization or authentication system your app might use.
113
123
  We use CanCan, so here's an example of how we do that:
114
124
 
115
125
  ```ruby
116
126
  class PagesController < ApplicationController
117
127
  include Cadmus::PagesController
118
-
128
+
119
129
  authorize_resource :page
120
-
130
+
121
131
  protected
122
132
  def page_class
123
133
  Page
@@ -130,7 +140,7 @@ class Ability
130
140
  def initialize(user)
131
141
  can :read, Page
132
142
  return unless user
133
-
143
+
134
144
  # in this example, we've added an owner_id column to our Page model
135
145
  can :manage, Page, :owner_id => user.id
136
146
  end
@@ -154,7 +164,7 @@ DugoutCoach::Application.routes.draw do
154
164
  resources :players
155
165
  resources :schedule
156
166
  end
157
-
167
+
158
168
  cadmus_pages # for global pages on your site
159
169
  end
160
170
  ```
@@ -177,20 +187,20 @@ end
177
187
  ```
178
188
 
179
189
  Now you have a way of separating team-specific pages from global pages on the site. The URLs for these pages might be,
180
- for example, http://dugoutcoach.net/teams/cosmonauts/directions, or
190
+ for example, http://dugoutcoach.net/teams/cosmonauts/directions, or
181
191
  http://dugoutcoach.net/teams/cosmonauts/promotions/free-hat-day (remember, Cadmus slugs can contain slashes). We'll
182
192
  now need a TeamPages controller to handle these:
183
193
 
184
194
  ```ruby
185
195
  class TeamPagesController < ApplicationController
186
196
  include Cadmus::PagesController
187
-
197
+
188
198
  self.page_parent_class = Team # page's parent is a Team
189
199
  self.page_parent_name = "team" # parent ID is in params[:team_id]
190
200
  self.find_parent_by = "slug" # parent ID is the Team's "slug" field rather than "id"
191
-
201
+
192
202
  authorize_resource :page
193
-
203
+
194
204
  protected
195
205
  def page_class
196
206
  Page
@@ -215,7 +225,7 @@ DugoutCoach::Application.routes.draw do
215
225
  resources :schedule
216
226
  cadmus_pages :controller => :team_pages, :shallow => true
217
227
  end
218
-
228
+
219
229
  cadmus_pages
220
230
  end
221
231
  ```
@@ -245,7 +255,7 @@ help them by providing them with a Liquid template variable they can use like so
245
255
 
246
256
  ```html
247
257
  <h1>We're the Cosmonauts!</h1>
248
-
258
+
249
259
  <p>Our uniform color this week is {{ team.uniform_color }}!</p>
250
260
  ```
251
261
 
@@ -254,19 +264,19 @@ To do this, you'll need to expose `team` as a Liquid assign variable:
254
264
  ```ruby
255
265
  class TeamPagesController < ApplicationController
256
266
  include Cadmus::PagesController
257
-
267
+
258
268
  self.page_parent_class = Team # page's parent is a Team
259
269
  self.page_parent_name = "team" # parent ID is in params[:team_id]
260
270
  self.find_parent_by = "slug" # parent ID is the Team's "slug" field rather than "id"
261
-
271
+
262
272
  authorize_resource :page
263
-
273
+
264
274
  protected
265
275
 
266
276
  def page_class
267
277
  Page
268
278
  end
269
-
279
+
270
280
  def liquid_assigns
271
281
  { :team => @page.parent }
272
282
  end
@@ -285,10 +295,66 @@ class Team < ActiveRecord::Base
285
295
  # everything else in your model...
286
296
  end
287
297
  ```
288
-
298
+
289
299
  You could also define a `to_liquid` method that returns a `Liquid::Drop` subclass for Teams, if you need to do things
290
300
  more complicated than just return data values.
291
301
 
302
+ ## Liquid Templates on Non-Page Models
303
+
304
+ Let's say you have another model in your app that you'd like to put a Liquid template on. For example, perhaps the baseball teams would like to send out a welcome email to their fans. You might create a `WelcomeEmail` model with a `content` field.
305
+
306
+ Cadmus provides a convenience mixin to let you make that field a Liquid template. You can use it like so:
307
+
308
+ ```ruby
309
+ class WelcomeEmail < ActiveRecord::Base
310
+ include Cadmus::LiquidTemplateField
311
+
312
+ liquid_template_field :content_liquid_template, :content
313
+
314
+ belongs_to :team
315
+ end
316
+ ```
317
+
318
+ Now if you call `my_welcome_email.content_liquid_template`, you'll get a parsed `Liquid::Template` generated from the value of `my_welcome_email.content`. You could further make the WelcomeEmail into a `Cadmus::Renderable` to make it render the template:
319
+
320
+ ```ruby
321
+ class WelcomeEmail < ActiveRecord::Base
322
+ include Cadmus::LiquidTemplateField
323
+ include Cadmus::Renderable
324
+
325
+ liquid_template_field :content_liquid_template, :content
326
+
327
+ belongs_to :team
328
+
329
+ def rendered_content
330
+ cadmus_renderer.render(content_liquid_template, :html)
331
+ end
332
+ end
333
+ ```
334
+
335
+ Presto! Now you can call `my_welcome_email.rendered_content`. Since `WelcomeEmail` includes `Cadmus::Renderable`, you can also define `liquid_assigns` to expose variables to the template, for example:
336
+
337
+ ```ruby
338
+ class WelcomeEmail < ActiveRecord::Base
339
+ include Cadmus::LiquidTemplateField
340
+ include Cadmus::Renderable
341
+
342
+ liquid_template_field :content_liquid_template, :content
343
+
344
+ belongs_to :team
345
+
346
+ def rendered_content
347
+ cadmus_renderer.render(content_liquid_template, :html)
348
+ end
349
+
350
+ def liquid_assigns
351
+ { 'team' => team }
352
+ end
353
+ end
354
+ ```
355
+
356
+ And now the welcome email templates can include `{{ team.name }}` and any other things derived from the Team model they want.
357
+
292
358
  ## Copyright and Licensing
293
359
 
294
- Copyright &copy; 2011-2012 Gively, Inc. Cadmus is released under the MIT license. For more information, see the LICENSE file.
360
+ Copyright &copy; Gively, Inc. Cadmus is released under the MIT license. For more information, see the LICENSE file.
data/lib/cadmus.rb CHANGED
@@ -3,7 +3,9 @@ require "cadmus/routing"
3
3
  require "cadmus/renderers"
4
4
  require "cadmus/controller_extensions"
5
5
  require "cadmus/slugs"
6
+ require "cadmus/liquid_template_field"
6
7
  require "cadmus/page"
8
+ require "cadmus/tags"
7
9
  require "rails"
8
10
 
9
11
  module Cadmus
@@ -221,5 +221,15 @@ module Cadmus
221
221
  raise ActiveRecord::RecordNotFound.new("No page called #{params[:page_glob]}") unless @page
222
222
  end
223
223
  end
224
+
225
+ def liquid_registers
226
+ registers = { 'parent' => page_parent }
227
+
228
+ if defined?(super)
229
+ registers.merge(super)
230
+ else
231
+ registers
232
+ end
233
+ end
224
234
  end
225
235
  end
@@ -0,0 +1,19 @@
1
+ module Cadmus
2
+ module LiquidTemplateField
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def liquid_template_field(method_name, field_name)
7
+ define_method method_name do
8
+ content = send(field_name)
9
+
10
+ begin
11
+ Liquid::Template.parse(content)
12
+ rescue Exception => exception
13
+ Liquid::Template.parse("#{exception.class.name}: #{exception.message}")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/cadmus/page.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  require 'liquid'
2
2
 
3
3
  module Cadmus
4
-
4
+
5
5
  # Adds a +cadmus_page+ extension method to ActiveRecord::Base that sets up a class as a page-like object for
6
6
  # Cadmus.
7
7
  module Page
8
8
  extend ActiveSupport::Concern
9
-
9
+ include Cadmus::LiquidTemplateField
10
+
10
11
  module ClassMethods
11
-
12
+
12
13
  # Sets up a model to behave as a Cadmus page. This will add the following behaviors:
13
14
  #
14
15
  # * A slug and slug generator field using HasSlug
@@ -33,19 +34,15 @@ module Cadmus
33
34
  self.name_field = (options.delete(:name_field) || :name).to_s
34
35
 
35
36
  belongs_to :parent, :polymorphic => true
36
-
37
+
37
38
  validates_presence_of name_field
38
39
  validates_uniqueness_of slug_field, :scope => [:parent_id, :parent_type]
39
40
  validates_exclusion_of slug_field, :in => %w(pages edit)
40
-
41
+
41
42
  scope :global, lambda { where(:parent_id => nil, :parent_type => nil) }
42
-
43
- class_eval do
44
- def liquid_template
45
- Liquid::Template.parse(content)
46
- end
47
- end
48
- end
43
+
44
+ liquid_template_field :liquid_template, :content
45
+ end
49
46
  end
50
47
  end
51
48
  end
@@ -0,0 +1,35 @@
1
+ module Cadmus
2
+ module Tags
3
+ class PageUrl < ::Liquid::Tag
4
+ class << self
5
+ attr_reader :page_path_method
6
+
7
+ def define_page_path_method(&page_path_method)
8
+ @page_path_method = page_path_method
9
+ end
10
+ end
11
+
12
+ attr_reader :page_name
13
+
14
+ def initialize(tag_name, args, tokens)
15
+ super
16
+ @page_name = args.strip
17
+ end
18
+
19
+ def render(context)
20
+ unless self.class.page_path_method
21
+ return "Error: #{self.class.name}.page_path_method is not defined. Please call #{self.class.name}.define_page_path_method in an initializer."
22
+ end
23
+
24
+ begin
25
+ parent = context.registers['parent']
26
+ Rails.application.routes.url_helpers.instance_exec(page_name, parent, &self.class.page_path_method)
27
+ rescue Exception => e
28
+ e.message
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ Liquid::Template.register_tag('page_url', Cadmus::Tags::PageUrl)
@@ -1,3 +1,3 @@
1
1
  module Cadmus
2
- VERSION = "0.5.3"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cadmus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nat Budin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-04 00:00:00.000000000 Z
12
+ date: 2017-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -65,11 +65,13 @@ files:
65
65
  - cadmus.gemspec
66
66
  - lib/cadmus.rb
67
67
  - lib/cadmus/controller_extensions.rb
68
+ - lib/cadmus/liquid_template_field.rb
68
69
  - lib/cadmus/markdown.rb
69
70
  - lib/cadmus/page.rb
70
71
  - lib/cadmus/renderers.rb
71
72
  - lib/cadmus/routing.rb
72
73
  - lib/cadmus/slugs.rb
74
+ - lib/cadmus/tags.rb
73
75
  - lib/cadmus/version.rb
74
76
  - lib/generators/cadmus/views_generator.rb
75
77
  homepage: http://github.com/gively/cadmus