Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.bundle/
log/*.log
pkg/
.idea/
spec/dummy/db/*.sqlite3
spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--require rails_helper
--require spec_helper
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gem 'rails'
gem 'jquery-rails'

group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'sqlite3'
gem 'database_cleaner'
end
137 changes: 137 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.0)
actionpack (= 4.2.0)
actionview (= 4.2.0)
activejob (= 4.2.0)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.0)
actionview (= 4.2.0)
activesupport (= 4.2.0)
rack (~> 1.6.0)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
actionview (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
activejob (4.2.0)
activesupport (= 4.2.0)
globalid (>= 0.3.0)
activemodel (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
activerecord (4.2.0)
activemodel (= 4.2.0)
activesupport (= 4.2.0)
arel (~> 6.0)
activesupport (4.2.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.0)
builder (3.2.2)
database_cleaner (1.4.0)
diff-lcs (1.2.5)
erubis (2.7.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
globalid (0.3.0)
activesupport (>= 4.1.0)
hike (1.2.3)
i18n (0.7.0)
jquery-rails (4.0.3)
rails-dom-testing (~> 1.0)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.2)
loofah (2.0.1)
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.4.3)
mini_portile (0.6.2)
minitest (5.5.1)
multi_json (1.10.1)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
rack (1.6.0)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.0)
actionmailer (= 4.2.0)
actionpack (= 4.2.0)
actionview (= 4.2.0)
activejob (= 4.2.0)
activemodel (= 4.2.0)
activerecord (= 4.2.0)
activesupport (= 4.2.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.0)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.5)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.1)
loofah (~> 2.0)
railties (4.2.0)
actionpack (= 4.2.0)
activesupport (= 4.2.0)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rspec-core (3.1.7)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
sprockets (2.12.3)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.2.4)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.10)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
database_cleaner
factory_girl_rails
jquery-rails
rails
rspec-rails (~> 3.0)
sqlite3
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2015 Alexey

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@

[ckeditor]: https://github.com/galetahub/ckeditor

SimpleEditable
===========================
This gem allow you to edit simple html text and object fields inline, on click.

```ruby
# Gemfile

gem 'simple_editable', :git => '[email protected]:Rezonans/simple-editable.git'
```
Install it with:

```bash
rails g simple_editable:install
```

Then run `rake db:migrate`

### Allow your views to use simple-editable helpers:

```ruby
class ApplicationController < ActionController::Base
helper SimpleEditable::ApplicationHelper
end

```

Configuration
-------------
After installing you get initialize file: `config/initializers/simple_editable.rb`
Edit it according to your settings

```ruby
# This setting changes the method which Simple Editable calls
# (within the application helper) to return the user who can access to inline editing.
#
# If false set, it allow anybody to use it

SimpleEditable.admin_user = false

# Simple Editable will automatically call an authentication
# method in a before filter of all controller actions to
# ensure that there is a currently logged in admin user.
#
# This setting changes the method which Simple Editable calls
# within the application controller.
#
# If false set, authentication method will not be called

SimpleEditable.authentication_method = false
```

Now you can use it with two helpers
`inline_object` and `inline_text`

Examples of usage
-------------

```ruby
#inline_object(tag, object, attribute, options = {})
inline_object(:div, obj, :field, class: 'hello-class another-class')

#inline_text(tag, key, default_value, options = {})
inline_text(:div, 'pages.home.title', 'Hello World', class: 'hello-class another-class', position: 'right')

#Allowed values for :position in %w(left, right, top, bottom)

```

When you сlick on the appropriate tag, you can edit it, with html attribute `contenteditable = true` and when focus is lost, data will send and saved.

Customizing
-------------
You can change the default behavior of editing with editing file `app/assets/javascripts/simple_editable/init.js` which generates on installing gem.

Also you can use any wysiwyg editor, which has the ability to apply editor inline (on tags with `contenteditable = true`).

One of them is [CKEditor][ckeditor]. Just install it by documentation. Inline Editing is enabled directly in it on HTML elements through the HTML5 contenteditable attribute.
36 changes: 36 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SimpleEditable'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'


load 'rails/tasks/statistics.rake'



Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'spec/**/*_test.rb'
t.verbose = false
end

task default: :test
11 changes: 11 additions & 0 deletions app/controllers/simple_editable/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SimpleEditable
class ApplicationController < ActionController::Base
before_filter :simple_editable_authentication_method

private

def simple_editable_authentication_method
SimpleEditable.authentication_method ? send(SimpleEditable.authentication_method) : true
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/simple_editable/inline_objects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SimpleEditable
class InlineObjectsController < ApplicationController
def update
klass = params[:class_name].constantize
object = klass.find(params[:id])
object.send(:write_attribute, params[:attribute], params[:value])
object.save(validate: false)
head :ok
end
end
end
9 changes: 9 additions & 0 deletions app/controllers/simple_editable/inline_texts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SimpleEditable
class InlineTextsController < ApplicationController
def update
value = params[:text_only] == 'true' ? ActionController::Base.helpers.sanitize(params[:value], tags: ['br']) : params[:value]
SimpleEditable::InlineTextStorage.new.update(params[:pk], value)
head :ok
end
end
end
60 changes: 60 additions & 0 deletions app/helpers/simple_editable/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module SimpleEditable
module ApplicationHelper
# Helper method for inline editing
# Example of Usage:
# = inline_text(:div, 'pages.home.title', 'Hello World', class: 'hello-class another-class', position: 'right')
# Allowed values for :position in %w(left, right, top, bottom)
def inline_text(tag, key, default_value, options = {})
@storage ||= InlineTextStorage.new
if simple_editable_admin_user.present?
opts = {
'contenteditable' => 'true',
'object' => 'false',
'data-text-only' => options[:text_only],
'data-pk' => key,
'data-url' => simple_editable.update_inline_text_path,
'class' => options[:class]
}

opts['data-placement'] = options[:position] if options[:position]

content_tag(tag, opts) do
raw(@storage.find_or_create(key, default_value))
end
else
content_tag(tag, class: "#{options[:class]}") do
raw(@storage.find_or_create(key, default_value))
end
end
end

def inline_object(tag, object, attribute, options = {})
if simple_editable_admin_user.present?
opts = {
'contenteditable' => 'true',
'object' => 'true',
'data-attribute' => attribute,
'data-id' => object.id,
'data-class' => object.class.to_s,
'data-url' => simple_editable.update_inline_object_path,
'class' => options[:class],
'data-text-only' => options[:text_only],
}

opts['data-placement'] = options[:position] if options[:position]

content_tag(tag, opts) do
raw(object.send(attribute))
end
else
content_tag(tag, class: "#{options[:class]}") do
raw(object.send(attribute))
end
end
end

def simple_editable_admin_user
SimpleEditable.admin_user ? send(SimpleEditable.admin_user) : true
end
end
end
Loading