effective_regions 1.4.10 → 1.4.11

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: acdd2fcfad642191327de27d5a9fe4aadb40c641
4
- data.tar.gz: 728f38432fd882671bac83c29d6e54ada4bb2249
3
+ metadata.gz: 483fd3d07ea6577b19b36977370babd10af15ae8
4
+ data.tar.gz: c9fe3624dc933c53f563031780ec8dc53b9b0cdd
5
5
  SHA512:
6
- metadata.gz: 5be4ebc7c19fd7ae234b09f20ca51b80fa57a53cd2ed619e643096764819fccb90a6c6f43801be3cf6ae6de1e6efeadfb851161134d1425bc81f2aba9c2e0b3f
7
- data.tar.gz: cb1d4675193faf95635840dc263b5405c062185a5fbf6d10487366d4f01d8b6afd1609b8ebac59f42db75fb90dc8f3445a7c3c781128bb7e903ab3a7e0f86921
6
+ metadata.gz: c8fff7cc51e8132edf5a336080b9b501606004c56dc1eb68cb7f551d9ed4ea1ab1e57df7a19b07a0c05189d70c677c4ff02508246761f25b3ed901f8dcd319c1
7
+ data.tar.gz: 7665b9df3050856b0270d34a41e6e19a0811fe080a7103b67ce22d2063e165752899a8110c8e57cce3afaa10116ed6078f776fed70b7aff71b4c1d08b7b86fae
data/README.md CHANGED
@@ -424,10 +424,17 @@ module Effective
424
424
  module Snippets
425
425
  class Post < Snippet
426
426
  attribute :post_id, Integer
427
+ attribute :all_posts, Array # We're going to assign this through the effective_region :snippet_locals option
427
428
 
428
429
  def post_object
429
430
  # We're using ::Post to refer to the app/models/post.rb rather than the Effective::Snippets::Post
430
- @post ||= ::Post.find_by_id(post_id)
431
+ @post ||= begin
432
+ if all_posts.present?
433
+ all_posts.find { |post| post.id == post_id }
434
+ else
435
+ ::Post.find_by_id(post_id)
436
+ end
437
+ end
431
438
  end
432
439
 
433
440
  end
@@ -532,11 +539,12 @@ We can pre-populate an effective_region's default content with some posts. Thes
532
539
  ```haml
533
540
  %h2 Posts
534
541
 
535
- = snippet_effective_region :sidebar_posts, :snippets => [:post] do
542
+ = snippet_effective_region :sidebar_posts, :snippets => [:post], :snippet_locals => {:all_posts => Post.all.to_a} do
536
543
  - Post.order(:created_at).first(5).each do |post|
537
- = render_snippet Effective::Snippets::Post.new(:post_id => post.id)
544
+ = render_snippet Effective::Snippets::Post.new(:post_id => post.id, :all_posts => [post])
538
545
  ```
539
546
 
547
+ Using the `snippet_locals` approach as above prevents an N+1 query when each snippet has to look up its own post.
540
548
 
541
549
  ### Summary
542
550
 
@@ -549,9 +557,7 @@ Templates are small pieces of reusable HTML that can be inserted into an `effect
549
557
 
550
558
  Unlike snippets, there are no configurable options or anything. They're just pieces of raw HTML that can be dropped in and then immediately editted.
551
559
 
552
- While handy, they were implemented as a bit of an after-thought, and will probably be refactored in future versions of effective_regions.
553
-
554
- They take the form of two files, a model and a view.
560
+ They take the form of three files, a model, a view, and an optional image file.
555
561
 
556
562
  ### The Model
557
563
 
@@ -587,6 +593,15 @@ The view is defined at app/models/effective/templates/_two_column.html.haml
587
593
  %p Right column
588
594
  ```
589
595
 
596
+ ### The Image
597
+
598
+ The image is used as an icon on the 'Content Templates' dialog screen.
599
+
600
+ It is optional, but will raise a silent 404 error if it doesn't exist.
601
+
602
+ The image should be 100x70 pixel .png file stored at /app/assets/images/effective/templates/two_column.png
603
+
604
+
590
605
  ## License
591
606
 
592
607
  MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
@@ -48,7 +48,7 @@ module Effective
48
48
  region.content = cleanup(vals[:content])
49
49
 
50
50
  region.snippets = HashWithIndifferentAccess.new()
51
- (vals[:snippets] || []).each { |snippet, vals| region.snippets[snippet] = vals }
51
+ (vals[:snippets] || []).each { |snippet, vals| region.snippets[snippet] = HashWithIndifferentAccess.new(vals.to_h) }
52
52
 
53
53
  # Last chance for a developer to make some changes here
54
54
  refresh_page = true if (run_before_save_method(region, regionable) rescue nil) == :refresh
@@ -43,6 +43,7 @@ module EffectiveRegionsHelper
43
43
  obj = args.first
44
44
  title = args.last.to_s.parameterize
45
45
  editable_tag = options.delete(:editable_tag) || :div
46
+ snippet_locals = options.delete(:snippet_locals) # These get passed into the Snippet.new() model
46
47
 
47
48
  # Set up the editable div options we need to send to ckeditor
48
49
  if effectively_editing?
@@ -76,19 +77,19 @@ module EffectiveRegionsHelper
76
77
 
77
78
  if effectively_editing? && (can_edit && options[:editable] != false) # If we need the editable div
78
79
  content_tag(editable_tag, opts) do
79
- region.try(:content).present? ? render_region(region, true) : (capture(&block) if block_given?)
80
+ region.try(:content).present? ? render_region(region, true, snippet_locals) : (capture(&block) if block_given?)
80
81
  end
81
82
  else
82
- region.try(:content).present? ? render_region(region, false) : (capture(&block) if block_given?)
83
+ region.try(:content).present? ? render_region(region, false, snippet_locals) : (capture(&block) if block_given?)
83
84
  end
84
85
  end
85
86
 
86
- def render_region(region, can_edit = true)
87
+ def render_region(region, can_edit = true, snippet_locals = {})
87
88
  return '' unless region
88
89
 
89
90
  region.content.tap do |html|
90
91
  html.scan(/\[(snippet_\d+)\]/).flatten.uniq.each do |id| # find snippet_1 and replace with snippet content
91
- snippet = region.snippet_objects.find { |snippet| snippet.id == id }
92
+ snippet = region.snippet_objects(snippet_locals).find { |snippet| snippet.id == id }
92
93
  html.gsub!("[#{id}]", render_snippet(snippet, can_edit)) if snippet
93
94
  end
94
95
  end.html_safe
@@ -23,11 +23,12 @@ module Effective
23
23
  # Hash of the Snippets objectified
24
24
  #
25
25
  # Returns a Hash of {'snippet_1' => CurrentUserInfo.new(snippets[:key]['options'])}
26
- def snippet_objects
26
+ def snippet_objects(locals = {})
27
+ locals = {} unless locals.kind_of?(Hash)
27
28
  @snippet_objects ||= snippets.map do |key, snippet| # Key here is 'snippet_1'
28
29
  if snippet['class_name']
29
30
  klass = "Effective::Snippets::#{snippet['class_name'].classify}".safe_constantize
30
- klass.new(snippet.merge!(:region => self, :id => key)) if klass
31
+ klass.new(snippet.merge!(locals).merge!(:region => self, :id => key)) if klass
31
32
  end
32
33
  end.compact
33
34
  end
@@ -31,6 +31,10 @@ module Effective
31
31
  end
32
32
  end
33
33
 
34
+ # If you define "attribute :something, Array" in your derived class
35
+ # You can call effective_region post, :content :snippet_locals => {:something => [1,2,3]}
36
+ # And it will be assigned when the effective_region is rendered
37
+
34
38
  def initialize(atts = {})
35
39
  (atts || {}).each { |k, v| self.send("#{k}=", v) if respond_to?("#{k}=") }
36
40
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveRegions
2
- VERSION = '1.4.10'.freeze
2
+ VERSION = '1.4.11'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_regions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.10
4
+ version: 1.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails