cmsable 0.0.9 → 0.4.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Nzc0ZDhiMTI0NjI3NzRmZDNhOTY1YWM3YzM4Yjc2OWYyYjM4NDVhNw==
4
+ ZDExMzlmMDNmOTdmODBjNGYyM2NjNzBhMjgxZjliMGYxODY0MWMxMQ==
5
5
  data.tar.gz: !binary |-
6
- NTc4ZmM3ZDQ1NzY2Njc0MTYxOWNiOGVjYzRjMGQ4YjM4MDA2NjI4NQ==
6
+ ZDhiZmQwYzNlZjgwODU2NjI5OWRiZjI0NTljMzQ5YWViMzFmMWJjNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjhiYzQ0NWIyZGYzOWM4OWQzN2UyZTIxZjQ3MmYyMWM1MzMwYTA3NzBlMjI3
10
- N2I3OTY2NDNiODlmNGVmM2Q5MjMwYWNmNDA2Mjk1NDY5YTk0MDFkOTY0NjBh
11
- OTI2MjYxODhhMTY3MjU2MzI4NDNlY2Y5ODZlZGIyNzQxYWNmYjc=
9
+ YjM2YmY0MDRjZmYwMTBlYmQwNTBlY2M1NzdhN2I1OTQ0NDk2YjkzY2IwNmVm
10
+ ZGEzMjgwYjcyMzlkNTgzNzU3NDkwNTgxN2M0MmEzMjAyNDIzMzRlYTk4N2Ni
11
+ ZjZiNWNlOTBmNzRmZmIwYWFiZDcxOTZhMzkzMDZhZmVhOWViMGU=
12
12
  data.tar.gz: !binary |-
13
- YjkxZjFlNmViYjMzMTU2MzNjOGNlMTBjY2EyNDZkOTY5MWEzYTk5MjI3Yjhh
14
- OTFhYjQ1YzlhODc4MTVkNzg4MGVhOTg4NTk5NzZkZDUwZTg5NDEwM2M4MWVk
15
- YTA0ZjkzZmIyMWE4ZTNiYTJiMjI1OTcyNmU4M2I5YTMyZGNmZDY=
13
+ OTQ3Y2Q1ZjQ4Yzc1NmIxZDdjMDM0MmY1NzBiYTdmMzY5MWQyZDRmNTVmNDkw
14
+ Y2Y2ZDczZDhiYjZiYzljMjExNDk1YmMxZTVlNmNlN2I1NmRkNTczY2IxZTli
15
+ YzdkZmEzMGRjYmUxYjBhZWUzNWIwNjA3N2E2ZTI0ZDJlOTUzYzM=
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Cmsable
2
+ ---------
3
+
4
+ Add inline editable content to pages.
5
+
6
+ ## Installation
7
+
8
+ Add to your Gemfile
9
+
10
+ ```ruby
11
+ gem 'cmsable'
12
+ ```
13
+
14
+ Get Dependencies
15
+
16
+ bundle install
17
+
18
+ Add to your routes
19
+
20
+ ```ruby
21
+ mount Cmsable::Engine => '/cmsable'
22
+ ```
23
+
24
+ Install and run the migrations if you want to use the built in model
25
+
26
+ rake cmsable:install:migrations
27
+ rake db:migrate
28
+
29
+ Cmsable uses CanCan so make sure this is set up in your necessary controller(s)
30
+ If your users aren't called users then you'll get something like:
31
+
32
+ undefined local variable or method `current_user' for ...
33
+
34
+ You need to define the current_ability, e.g.
35
+
36
+ ```ruby
37
+ def current_ability
38
+ @current_ability ||= AccountAbility.new(current_account)
39
+ end
40
+ ```
41
+ (From https://github.com/ryanb/cancan/wiki/changing-defaults)
42
+
43
+ ## Usage
44
+
45
+ Add to your layout:
46
+
47
+ ```ruby
48
+ <%= cmsable_assets %>
49
+ ```
50
+
51
+ ### To use the built in model
52
+ In your view files
53
+
54
+ ```ruby
55
+ <%= cmsable 'identifier' %>
56
+ ```
57
+
58
+ Identifier can be any string or symbol, for example `'Homepage Intro'` or `:something`
59
+
60
+ For plain text areas pass `plain: true` as an option.
61
+ This way you can put content in any tag you want without worrying about users
62
+ messing up the markup.
63
+
64
+ ```ruby
65
+ <span><%= cmsable :identifier, plain: true %></span>
66
+ ```
67
+
68
+ The problem with the above is that in edit mode you'll end up with invalid HTML:
69
+
70
+ ```html
71
+ <span><div contenteditable>Content</div></span>
72
+ ```
73
+
74
+ To avoid this you can override the tag used in edit mode:
75
+
76
+ ```ruby
77
+ <%= cmsable :identifier, plain: true, tag: 'span' %>
78
+ ```
79
+ Resulting in:
80
+ ```html
81
+ <span contenteditable>Content</span>
82
+ ```
83
+
84
+ ### To use your own models
85
+ Add to your model:
86
+
87
+ ```ruby
88
+ cmsable :body => :column_to_use_for_content
89
+ ```
90
+
91
+ Then in your views:
92
+ ```ruby
93
+ <%= cmsable @model_instance %>
94
+ ```
95
+
96
+ ## TODO
97
+ - Handle saving errors
98
+ - Remove CanCan dependancy
99
+ - Decouple ckeditor
100
+ - Support different editors
101
+ - Setup config initializer for added customizability
102
+ - Add file mode
103
+ - Add image mode
104
+ - Add video mode (youtube/vimeo/etc)
105
+ - Add support for langs/i18n
106
+ - Fix highlight so empty areas are still visible
@@ -8,7 +8,9 @@ $(function() {
8
8
  $('#cmsable_control a').toggleClass('cmsable_on');
9
9
  if ($(this).hasClass('cmsable_on')) {
10
10
  $('.cmsable_editor').addClass('cmsable_reveal').attr('contenteditable', true);
11
- CKEDITOR.inlineAll();
11
+ $('.cmsable_rich').each(function(idx, ele) {
12
+ CKEDITOR.inline(ele);
13
+ });
12
14
  }else{
13
15
  $('.cmsable_editor').removeClass('cmsable_reveal').attr('contenteditable', false);
14
16
  $.each(CKEDITOR.instances, function(index, instance) {
@@ -20,29 +22,44 @@ $(function() {
20
22
  $(document).on('click', '.cmsable_save.cmsable_on', function() {
21
23
  $('#cmsable_control').addClass('processing');
22
24
  var that = this,
23
- updated = 0,
24
- instances = 0;
25
+ token = that.getAttribute('data-token');
26
+
27
+ var cmsables = [];
28
+
29
+ // Submit plain text fields
30
+ $('.cmsable_plain').each(function(idx, ele) {
31
+ $ele = $(ele);
32
+ cmsables.push({
33
+ id: ele.id.replace(/\D/g, ''),
34
+ model: $ele.data('model'),
35
+ body: $ele.text()
36
+ });
37
+ });
38
+
39
+ // Submit CKEditor fields
25
40
  $.each(CKEDITOR.instances, function(index, instance) {
26
- instances++;
27
- var id = instance.name.replace(/\D/g, ''),
28
- model = instance.element.$.getAttribute('data-model'),
29
- token = that.getAttribute('data-token'),
30
- body = instance.getData();
31
- $.post('/cmsable/' + id, {
41
+ cmsables.push({
42
+ id: instance.name.replace(/\D/g, ''),
43
+ model: instance.element.$.getAttribute('data-model'),
44
+ body: instance.getData()
45
+ });
46
+ });
47
+
48
+ $.each(cmsables, function(idx, cmsable) {
49
+ $.post('/cmsable/' + cmsable.id, {
32
50
  _method: 'put',
33
- model: model,
51
+ model: cmsable.model,
34
52
  authenticity_token: token,
35
53
  content: {
36
- body: body
54
+ body: cmsable.body
37
55
  }
38
56
  }, function(data) {
39
- updated++;
40
- if (updated === instances) {
57
+ if (idx == cmsables.length - 1) {
41
58
  $('#cmsable_control').removeClass('processing');
42
59
  }
43
60
  });
44
61
  });
62
+
45
63
  $('.cmsable_edit').click();
46
64
  });
47
-
48
65
  });
@@ -1,32 +1,50 @@
1
+ # encoding: utf-8
1
2
  module Cmsable
3
+ # Cmsable view helpers
2
4
  module CmsableHelper
3
-
4
5
  def cmsable_assets
5
6
  javascript_include_tag('cmsable/application') +
6
7
  stylesheet_link_tag('cmsable/application')
7
8
  end
8
9
 
9
- def cmsable name_or_model, options = {}
10
+ TYPES = [
11
+ :rich, # CKEditor (default)
12
+ :plain # Nothing fancy
13
+ ]
14
+
15
+ DEFAULTS = {
16
+ readonly: false, # Can edit by default
17
+ type: :rich, # Use rich text editor by default
18
+ plantext: false, # No text editor
19
+ tag: nil # What tag to use for contenteditable
20
+ }
10
21
 
22
+ def cmsable(name_or_model, options = {})
11
23
  model = get_model name_or_model
12
24
 
13
- options = {
14
- readonly:false
15
- }.merge options
25
+ options = DEFAULTS.merge options
26
+
27
+ # Fail on unknow types
28
+ typefail options[:type] unless TYPES.include? options[:type]
16
29
 
17
- # Skip checking permission of set to readonly or
30
+ # Skip checking permission if set to readonly or
18
31
  # explicit authorisation is passed
19
- unless options.include?(:authorised) or options[:readonly]
20
- options[:authorised] = authorised?(model)
21
- end
32
+ readonly = options.include?(:authorised) || options[:readonly]
33
+ options[:authorised] = authorised?(model) unless readonly
22
34
 
23
- content_or_editable_content model, (options[:authorised] and !options[:readonly])
35
+ content = model.send(model.cmsable_body).html_safe
36
+ editable = options[:authorised] && !options[:readonly]
24
37
 
38
+ editable ? show_cmsable(content, model, options) : uncmsable(content, options[:tag])
25
39
  end
26
40
 
27
- private
41
+ private
42
+
43
+ def type_fail(type)
44
+ fail "Unknown cmsable type: #{type}." + "Must be one of #{TYPES * ', '}"
45
+ end
28
46
 
29
- def get_model name_or_model
47
+ def get_model(name_or_model)
30
48
  if name_or_model.class < ActiveRecord::Base
31
49
  name_or_model
32
50
  else
@@ -34,31 +52,33 @@ module Cmsable
34
52
  end
35
53
  end
36
54
 
37
- def content_or_editable_content model, editable
38
- control = if(!@cmsable_control_rendered and editable)
39
- render :template => 'cmsable/cmsable/control'
40
- end
55
+ def show_cmsable(content, model, options)
56
+ control = unless @cmsable_control_rendered
57
+ render template: 'cmsable/cmsable/control'
58
+ end
41
59
  @cmsable_control_rendered = true
42
- content = model.send(model.cmsable_body).html_safe
43
- if editable
44
- content_tag(:div, content, {
45
- class: :cmsable_editor,
46
- id: "cmsable_edit_#{model.class.to_s.parameterize}_#{model.id}",
47
- :'data-model' => model.class,
48
- }) + (control or '')
60
+ id = "cmsable_edit_#{model.class.to_s.parameterize}_#{model.id}"
61
+ content_tag((options[:tag] || :div), content,
62
+ class: "cmsable_editor cmsable_#{options[:type]}",
63
+ id: id,
64
+ 'data-model' => model.class,
65
+ ) + (control || '')
66
+ end
67
+
68
+ def uncmsable(content, tag)
69
+ if tag
70
+ content_tag tag, content
49
71
  else
50
72
  content
51
73
  end
52
74
  end
53
75
 
54
- def authorised? model
55
- authorised = false
76
+ def authorised?(model)
56
77
  if Cmsable.const_defined? :CanCan
57
- authorised = can? :update, model
78
+ can? :update, model
58
79
  else
59
- authorised = Cmsable.authorised
80
+ Cmsable.authorised
60
81
  end
61
82
  end
62
-
63
83
  end
64
84
  end
data/lib/cmsable.rb CHANGED
@@ -1,24 +1,21 @@
1
- require "cmsable/engine"
1
+ # encoding: utf-8
2
+ require 'cmsable/engine'
2
3
 
3
4
  module Cmsable
4
-
5
5
  mattr_accessor :authorised
6
6
 
7
+ # Add cmsable method to ActiveRecord classes
7
8
  module ClassMethods
8
- def cmsable options = {}
9
-
9
+ def cmsable(options = {})
10
10
  cattr_accessor :cmsable_body
11
11
 
12
12
  options = {
13
- :body => :body
13
+ body: :body
14
14
  }.merge options
15
15
 
16
16
  self.cmsable_body = options[:body]
17
-
18
17
  end
19
18
  end
20
-
21
19
  end
22
20
 
23
- # Add cmsable method to ActiveRecord classes
24
21
  ActiveRecord::Base.send :extend, Cmsable::ClassMethods
@@ -1,7 +1,8 @@
1
+ # encoding: utf-8
1
2
  module Cmsable
3
+ # This makes the consuming app load our helpers
2
4
  class Engine < ::Rails::Engine
3
5
  isolate_namespace Cmsable
4
- # This makes the consuming app load our helpers
5
6
  initializer 'cmsable.action_controller' do |app|
6
7
  ActiveSupport.on_load :action_controller do
7
8
  helper Cmsable::CmsableHelper
@@ -1,3 +1,3 @@
1
1
  module Cmsable
2
- VERSION = "0.0.9"
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmsable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Butler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-16 00:00:00.000000000 Z
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -75,7 +75,7 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - MIT-LICENSE
78
- - README.rdoc
78
+ - README.md
79
79
  - Rakefile
80
80
  - app/assets/images/cmsable/ajax-loader.gif
81
81
  - app/assets/images/cmsable/construction.png
data/README.rdoc DELETED
@@ -1,57 +0,0 @@
1
- # Cmsable
2
-
3
- Add inline editable content to pages.
4
-
5
- Installation:
6
-
7
- Add to your Gemfile
8
- gem 'cmsable'
9
-
10
- Get Dependencies
11
- bundle install
12
-
13
- Add to your routes
14
- mount Cmsable::Engine => '/cmsable'
15
-
16
- Install and run the migrations if you want to use the built in model
17
- rake cmsable:install:migrations
18
- rake db:migrate
19
-
20
- Cmsable uses CanCan so make sure this is setup in your necessary controller(s)
21
- If your users aren't called users then you'll get something like:
22
- undefined local variable or method `current_user' for ...
23
-
24
- You need to define the current_ability, e.g.
25
- def current_ability
26
- @current_ability ||= AccountAbility.new(current_account)
27
- end
28
- From https://github.com/ryanb/cancan/wiki/changing-defaults
29
-
30
- Usage:
31
-
32
- In your layout
33
- <%= cmsable_assets %>
34
-
35
- To use the built in model
36
- In your view files
37
- <%= cmsable 'identifier' %>
38
-
39
- Identifier can be anything, for example 'Homepage Intro' or something
40
-
41
- To use existing models
42
- Add to your model
43
- cmsable :body => :column_to_use_for_content
44
- Then in your views
45
- <%= cmsable @model_instance %>
46
-
47
- ## TODO
48
- - Handle saving errors
49
- - Remove CanCan dependancy
50
- - Decouple ckeditor
51
- - Support different editors
52
- - Setup config initializer for added customizability
53
- - Add plain text mode
54
- - Add file mode
55
- - Add image mode
56
- - Add video mode (youtube/vimeo/etc)
57
- - Fix highlight so empty areas are still visible