polyblock 0.6.0 → 0.6.1

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: a1cf49aeb0def2dcae5443ec3bc630aa55ce9284
4
- data.tar.gz: 28c7f391771bafffdf7c862f572e4518e0f9c23c
3
+ metadata.gz: 80795451e15edd4c4f2d7eb45fc9f023643d2ecc
4
+ data.tar.gz: 10c97e9da58822ff329b673c9f944ac43dbdd241
5
5
  SHA512:
6
- metadata.gz: 52032f72eebdaac1c5b5c89a1986ac19b76aa17ad7814368cd3823048a671613cd94ecd17a638e86da10d843022b6d9862591a46ea63c9faaec509c4f12d6496
7
- data.tar.gz: 8b38559e23d84ad39daad1e4dfaaa6265a26098e25b61e4b09239ffce8dd162f0f0024771473566d8fea7db8034a519a9615848359283a48c5d9147ad87fd5de
6
+ metadata.gz: da8686b6bcc954bdabb98776b1076f48274710124ccf6a8d297e05cbdcc449e3d6715b7bd31dd0e082c523a7935676394ff1756c3124cc0671c952f4058a193e
7
+ data.tar.gz: 37aeb629009db8eed40f3cab52f2ad0b0c7364f60784cf400502ca15db921d2f363fb895cdd23faaf36e9dc536076c8d4eb68aa07fda7bf2707b265101bab693
@@ -12,56 +12,41 @@ module Polyblock
12
12
  }.merge(options)
13
13
 
14
14
  # Fetch or create Polyblock
15
- if name.is_a? Polyblock::Block
16
- pb = name
17
- pb_id = pb.id
18
- name = pb.name
19
- pb_exists = true
20
- else
21
- matches = Polyblock::Block.where :name => name
22
- pb_exists = matches.any?
23
- pb = pb_exists ? matches.first : Polyblock::Block.new({:name => name})
24
- pb_id = if pb_exists
25
- pb.id
26
- else
27
- Polyblock::Block.any? ? Polyblock::Block.order(:id).last.id + 1 : 1
28
- end
29
- end
30
-
31
- # Generate a random string for id
32
- o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
33
- random_id = (0...50).map { o[rand(o.length)] }.join
15
+ pb = Block.fetch_or_create(name)
16
+ pb_id = pb.new_record? ? Block.next_id : pb.id
17
+ name = pb.name if name.is_a? Block
18
+ random_id = Block.random_id
34
19
 
35
20
  # Build output tag
36
- content = pb_exists ? pb.content : "<p>This block is content managed by Polyblock!<br />Click to edit me!</p>"
21
+ content = pb.content.present? ? pb.content : "[Editable content space for Polyblock <code>#{name}</code>]"
37
22
  tag_options = options[:tag_options].deep_merge({:id => "pb-#{pb_id}-#{random_id}", :data => {:pbid => pb_id, :pbname => name}})
38
23
  tag_options[:contenteditable] = if options[:condensed]
39
- "false"
24
+ 'false'
40
25
  elsif options.has_key?(:editable)
41
- options[:editable] ? "true" : "false"
26
+ options[:editable] ? 'true' : 'false'
42
27
  elsif respond_to?(:can?) && can?(:manage, pb)
43
- "true"
28
+ 'true'
44
29
  elsif respond_to?(:user_signed_in?) && user_signed_in?
45
30
  if current_user.respond_to?(:admin?) && current_user.admin?
46
- "true"
31
+ 'true'
47
32
  elsif pb.contentable.present? &&
48
33
  pb.contentable.respond_to?(:user_id) &&
49
34
  pb.contentable.user_id == current_user.id
50
- "true"
35
+ 'true'
51
36
  else
52
- "false"
37
+ 'false'
53
38
  end
54
39
  else
55
- "false"
40
+ 'false'
56
41
  end
57
- tag_options.delete(:contenteditable) if tag_options[:contenteditable] == "false"
58
- tag_options[:class] = (tag_options.has_key?(:class) ? "#{tag_options[:class]} ":"") + "polyblock"
59
- tag_options[:data][:pb_exists] = pb_exists
42
+ tag_options.delete(:contenteditable) if tag_options[:contenteditable] == 'false'
43
+ tag_options[:class] = (tag_options.key?(:class) ? "#{tag_options[:class]} ": '') + 'polyblock'
44
+ tag_options[:data][:pb_exists] = pb.new_record?
60
45
 
61
46
  # Truncation
62
47
  if options[:condensed]
63
48
  tag_options[:class] += " polyblock-condensed"
64
- content = CGI.unescapeHTML(content.gsub("<br><br>", "<br>").gsub('&nbsp;', ' '))
49
+ content = CGI.unescapeHTML(content.gsub('<br><br>', '<br>').gsub('&nbsp;', ' '))
65
50
  truncate(sanitize(strip_tags(content)), :length => options[:length], :omission => options[:omission])
66
51
  else
67
52
  content_tag(options[:tag], content, tag_options, false)
@@ -73,11 +58,11 @@ module Polyblock
73
58
  :label => false,
74
59
  :input_html => {}
75
60
  }.merge(options)
76
- render :partial => "polyblock/simple_fields_for", :locals => {:f => f, :name => name, :options => options}
61
+ render :partial => 'polyblock/simple_fields_for', :locals => {:f => f, :name => name, :options => options}
77
62
  end
78
63
 
79
64
  def polyblock_editor_bar
80
- render :partial => "polyblock/editor_bar"
65
+ render :partial => 'polyblock/editor_bar'
81
66
  end
82
67
 
83
68
  end
@@ -7,19 +7,15 @@ module Polyblock
7
7
 
8
8
  def self.import(pbs)
9
9
  outputs = pbs.map do |pb|
10
- matches = Block.where :name => pb[:name]
11
- if matches.any?
12
- m = matches.first
13
- m.update_attributes(pb) if m.content != pb[:content]
14
- else
15
- Block.create(pb)
16
- end
10
+ block = fetch_or_initialize pb[:name]
11
+ block.content = pb[:content] if pb.key?(:content) && pb[:content].present? && pb[:content] != block.content
12
+ block.save
17
13
  end
18
14
  outputs.include?(false)
19
15
  end
20
16
 
21
17
  def self.export
22
- output = Block.all.as_json(:except => [:id, :created_at, :updated_at])
18
+ output = all.as_json(:except => [:id, :created_at, :updated_at])
23
19
  puts ""
24
20
  puts "Run the following in Rails Console on the remote server:"
25
21
  puts ""
@@ -32,11 +28,37 @@ module Polyblock
32
28
  end
33
29
 
34
30
  def present?
35
- content.present?
31
+ !nil? && content.present?
36
32
  end
37
33
 
38
34
  def blank?
39
- content.blank?
35
+ nil? || content.blank?
36
+ end
37
+
38
+ def self.fetch_or_initialize(name)
39
+ if name.is_a? Block
40
+ name
41
+ else
42
+ matches = where :name => name
43
+ if matches.any?
44
+ matches.first
45
+ else
46
+ new({:name => name})
47
+ end
48
+ end
49
+ end
50
+ def self.fetch_or_create(name)
51
+ pb = fetch_or_initialize(name)
52
+ pb.save if pb.new_record?
53
+ pb
54
+ end
55
+
56
+ def self.next_id
57
+ any? ? maximum(:id).next : 1
58
+ end
59
+ def self.random_id
60
+ o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
61
+ (0...50).map { o[rand(o.length)] }.join
40
62
  end
41
63
 
42
64
  end
@@ -1,4 +1,5 @@
1
1
  = f.simple_fields_for name do |ff|
2
+
2
3
  = ff.input_field :name, :as => :hidden, :value => name.to_s
3
4
  - if !options[:label]
4
5
  = ff.input_field :content, options[:input_html].merge({:as => :ckeditor})
@@ -1,3 +1,3 @@
1
1
  module Polyblock
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -1,10 +1,10 @@
1
1
  %h1 Polyblock Test Dummy
2
2
 
3
3
  %h3 Polyblock with Inline Editing
4
- = pb "Test", :editable => true
4
+ = pb 'Test', :editable => true
5
5
 
6
6
  %h3 New Polyblock
7
- = pb "Neww", :editable => true
7
+ = pb Polyblock::Block.random_id, :editable => true
8
8
 
9
9
  %h3 Polyblock SimpleForm
10
10
  = simple_form_for @event do |f|
Binary file