Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
rewrote Readme; fixed Rails3.1 compatability - now works with 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Howard committed Sep 12, 2011
1 parent e4e139d commit 6d37145
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 152 deletions.
140 changes: 53 additions & 87 deletions README.markdown
@@ -1,48 +1,65 @@
UseTinyMCE
UseTinyMCE - 0.0.9
==========

**UseTinyMCE** is yet another (as if we needed another) hack for including
TinyMCE in a Rails 3.0 or 3.1 app.

**NOTE:** Rails 3.1.0 broke version 0.0.8. The current version works (for me)

It differs from the others that I looked at on rubygems in that:

1. It works with Rails [3.0](#rails_30_integration) & [3.1.0.rc4](#rails_31_integration),
1. It works with Rails [3.0](#rails_30_integration) & [3.1.0](#rails_31_integration),
details below
2. It's minimal. It does not provide any juicy Rails style configuration for
TinyMCE. You just use the config stuff which comes with TinyMCE - in javascript.

What's Provided
================
## Rails 3.0 & 3.1 Integration

It's the same in both cases. See [Step By Step](#step_by_step) below

## Integration with a Prototype based site
**Rails 3.1 Note:** TinyMCE doesn't play well with the asset pipeline, so it is installed
in public. This has to do with the way TinyMCE is architected: it is plugin based
and scatters javascript, css, and html all through it's plugin directory. It manages it's
own loading.

This brings in the 'advanced' version of TinyMCE.
## Selectively adding TinyMCE to selected text areas

Install using `rake use_tinymce:install_advanced` to get the 'advanced' configuration
initialization file.
TinyMCE supports selectively using TinyMCE in or not in different textareas within
the same page .

Or, you can install using `rake use_tinymce:install_simple` to use the 'simple' configuration.
To do this, proceed as above and for `textarea` elements that you do *not* want to
use TinyMCE in, add the class '`no-tinymce`'.

## Integration with jQuery
Read the comments in '`use_tinymce-gem-location/lib/tasks/use_tinymce.rake`' for details.
Or read the TinyMCE code.

This uses the TinyMCE jQuery plugin. See [jQuery Plugin](http://tinymce.moxiecode.com/wiki.php/jQuery_Plugin)
on the MoxieCode site for configuration details.
## Step by Step

Install using `rake use_tinymce:install_jquery`, which installs an `advanced` TinyMCE
configuration file which uses jQuery selectors.
1. You will need to install *TinyMCE* in your /public/javascript directory.
Do that by running one of these rake tasks:
* `rake use_tinymce:install_jquery` - if you are using jQuery (Rails 3.1 default)
* `rake use_tinymce:install_simple` - if you are using Prototype (Rails 3.0 default)
* `rake use_tinymce:install_advanced` - if you are using Prototype (Rails 3.0 default)

If you don't need much and aren't familiar with *TinyMCE*, then `rake use_tinymce:install_simple`
should be fine. The *advanced* version configures *TinyMCE* with many more features. Go to
the ["TinyMCE website for details"](http://tinymce.moxiecode.com/)
2. Add `use_tinymce args` to all the controllers for views containing **textarea** fields
in which you want to run *TinyMCE*. `args` should be:
* `:all` - to enable *TinyMCE* for all actions
* `:foo, :bar` - to enable *TinyMCE* only for views rendered by `foo` and `bar` actions
3. Add `<%= use_tinymce_link %>` to the `HEAD` section of you application layout - for *at least*
all pages which should use *TinyMCE*
4. (Optional) Edit `/public/javascript/use_tinymce_init.js` to customize your *TinyMCE* feature
set.

## Rails 3.0 Integration
That's it.

Rails 3.0 loads javascript files one at a time, so we can control whether
TinyMCE is used with a *textarea* by controlling when it is included.
## Supplied Methods

We do this invoking `use_tinymce(*actions)` in the controllers which display
pages on which we want to use TinyMCE. TinyMCE will be automatically applied
to all of the `textarea` elements of the selected pages.
Detailed info on the methods in this Gem.

This is controlled by using the view helper `use_tinymce_link` in the application layout
(or, for more complex situations, you can use the predicate `use_tinymce?(action)` and
some code you write)
As usual, read the source for the definitive, up-to-date truth.

### `use_tinymce(*actions)`

Expand All @@ -68,87 +85,36 @@ by a previous call to `use_tinymce` [or if you included `use_tinymce :all` in yo
`action` will usually come from `params[:action]`, so it's easier to use
`use_tinymce_link` which already does that.

### Step by Step Configuration

1. You will need to install *TinyMCE* in your /public/javascript directory.
Do that by running one of these rake tasks:
* `rake use_tinymce:install_simple`
* `rake use_tinymce:install_advanced`
* `rake use_tinymce:install_jquery`

If you don't need much and aren't familiar with *TinyMCE*, then `rake use_tinymce:install_simple`
should be fine. The *advanced* version configures *TinyMCE* with many more features. Go to
the ["TinyMCE website for details"](http://tinymce.moxiecode.com/)
2. Add `use_tinymce args` to all the controllers for views containing **textarea** fields
in which you want to run *TinyMCE*. `args` should be:
* `:all` - to enable *TinyMCE* for all actions
* `:foo, :bar` - to enable *TinyMCE* only for views rendered by `foo` and `bar` actions
3. Add `<%= use_tinymce_link %>` to the `HEAD` section of you application layout - for *at least*
all pages which should use *TinyMCE*
4. (Optional) Edit `/public/javascript/use_tinymce_init.js` to customize your *TinyMCE* feature
set.

That's it.

## Rails 3.1 Integration

Rails 3.1 adds the 'asset pipeline' by default. This uses the 'sprocket' gem
to combine javascript and css into two files as well as adding direct support
of Coffeescript and SASS.

It also mucks up TinyMCE because TinyMCE - by default - dynamically loads it's
plugins as they are needed. While the TinyMCE implementation is quite nice,
it and the asset pipeline mess each other up.

[It actually gets worse: TinyMCE has HTML and CSS files scattered throughout
it's `plugins` directories which are loaded dynamically as plugins pop up windows.]

There are a lot of ways to resolve this - but I've chosen a simple, brute force
approach: I stuff the TinyMCE distribution and my javascript glue in `public/javascripts`,
just like it is using Rails 3.0.x. Then I tried it with the asset pipeline enabled
and - drum role - it works.

### Step by Step Configuration

1. You will need to install *TinyMCE* in your /public/javascript directory.
Do that by running one of these rake tasks:
* `rake use_tinymce:install_simple`
* `rake use_tinymce:install_advanced`
* `rake use_tinymce:install_jquery`
2. Add the `tinymce` class to all the `textarea` elements in all views and partials
you want to apply TinyMCE to.

## Four rake tasks:

**NOTE:** Rails 3.1 automatically adds a rake task 'use_tinymce_engine:install:migrations'.
It doesn't do anything because there aren't any migrations for 'use_tinymce'.
Ignore it.

**NOTE** `assets` here refers to the `assets` directory in the `use_tinymce`
gem - **not** the Rails 3.1 asset pipeline directory.

Both copy the contents of the `assets/TinyMCE` directory to your rails
`public/javascripts` directory. They both also copy a TinyMCE initialization
script to `public/javascripts/use_tinymce_init.js`

This initialization scripts are copied literally from the TinyMCE website
This initialization scripts were copied literally from the TinyMCE website
["For Dummies" page](http://tinymce.moxiecode.com/wiki.php/%22For_Dummies%22)
- that is: *http://tinymce.moxiecode.com/wiki.php/%22For_Dummies%22*

`rake use_tinymce:install_advanced` copies `assets/use_tinymce_init_advanced.js` -
Prototype Versions:

* `rake use_tinymce:install_advanced` copies `use_tinymce/assets/use_tinymce_init_advanced.js`
and the contents of `use_tinymce/tinymce_no_jquery` -
which provides all the full blown features.

`rake use_tinymce:install_simple` copies `assets/use_tinymce_ini_simple.js` -
* `rake use_tinymce:install_simple` copies `use_tinymce/assets/use_tinymce_init_simple.js`
and the contents of `use_tinymce/tinymce_no_jquery` -
copies the bare bones version.

The jQuery version was copied from an example script and then slightly modified
[see the code comments].

`rake use_tinymce:install_jquery` copies `assets/use_tinymce_init_jquery.js` -
* `rake use_tinymce:install_jquery` copies `assets/use_tinymce_init_jquery.js`
and the contents of `use_tinymce/tinymce_jquery` -
which attaches an 'advanced' TinyMCE configuration to textareas.

`rake use_tinymce:uninstall` removes use_tinymce_init.js and the TinyMCE
directory from `public/javascripts`
Maintenance: if you want to get rid of `use_tinymce`, then:

* `rake use_tinymce:uninstall` removes use_tinymce_init.js and the TinyMCE
directory from your applications `public/javascripts`

Relocating *TinyMCE* or Something Else
==================
Expand Down
113 changes: 63 additions & 50 deletions lib/tasks/use_tinymce.rake
@@ -1,88 +1,101 @@
require 'fileutils'

class UseTinyMCE
# paths
USE_TINYMCE_ROOT = File.expand_path('../../../', __FILE__)
ASSETS_ROOT = File.join(USE_TINYMCE_ROOT, 'assets')
case
when Rails.version =~ /^3.0/ then
JAVASCRIPT_ROOT = File.join(Rails.root, 'public', 'javascripts')
RAILS_REGX = Regexp.new('//\s+Rails 3.0')
MODE_STRING = 'mode : "textareas"'
JQUERY_SELECTOR = '"textarea"'
when Rails.version =~ /^3.1/ then;
# make sure we are in the right versino of Rails
unless defined?(Rails)
puts "This rake task only runs on Rails"
exit
end
unless Rails.version =~ /^3.[01]/
puts "Don't know how to install on Rails Version #{Rails.version}"
exit
end

module UseTinyMCE
module RakeSupport
# gem paths - used to source for installation
USE_TINYMCE_ROOT = File.expand_path('../../../', __FILE__)
ASSETS_ROOT = File.join(USE_TINYMCE_ROOT, 'assets')

# target paths and customizations
# MSH - asset pipeline gets in way, so put it in public/javascripts
# JAVASCRIPT_ROOT = File.join(Rails.root, 'app', 'assets', 'javascripts')
JAVASCRIPT_ROOT = File.join(Rails.root, 'public', 'javascripts')
RAILS_REGX = Regexp.new('//\s+Rails 3.1')
MODE_STRING = [ 'mode: "specific_textareas"', ' editor_selector: "tinymce"' ].join(",\n")
# set the TinyMCE selection mode: this controls which textarea elements use TinyMCE
# mode: specific_textareas is essentially the same as mode:textareas
# editor_selector: foo defines a class which MUST be present in a textarea element IF
# (1) editor_selector is defined & (2) you want it to have TinyMCE
# editor_deselector: foo defines a CLASS which must be present to keep a textarea from
# using TinyMCE. Note: Both editor_deselector must be defined AND the class must be present.
# so - the settings below include TinyMCE in textareas By Default and Allow Deselecting
# Use this if you are both masochistic and want love to positively select which textareas
# use TinyMCE. This code
# MODE_STRING = [ 'mode: "specific_textareas"', ' editor_selector: "tinymce"',
# ' editor_deselector: "no-tinymce"' ].join(",\n")
MODE_STRING = [ 'mode: "specific_textareas"', ' editor_deselector: "no-tinymce"' ].join(",\n")
# This MODE_STRING will configure to NOT USE TinyMCE in textareas unless the 'editor_selector'
# class is present.
# MODE_STRING = [ 'mode: "specific_textareas"', ' editor_selector: "tinymce"' ].join(",\n")
JQUERY_SELECTOR = '"textarea.tinymce"'
else
puts "Don't know how to install on Rails Version #{Rails.version}"
exit
end
# puts "USE_TINYMCE_ROOT: #{USE_TINYMCE_ROOT}"
# puts "ASSETS_ROOT: #{ASSETS_ROOT}"
# puts "JAVASCRIPT_ROOT: #{JAVASCRIPT_ROOT}"

def self.copy_init_script(source)
source_path = File.join(ASSETS_ROOT, source)
source_text = File.new(source_path).read.sub(/{mode_string}/, MODE_STRING).sub(/{jquery_selector}/, JQUERY_SELECTOR)
def self.copy_init_script(source)
source_path = File.join(ASSETS_ROOT, source)
source_text = File.new(source_path).read.sub(/{mode_string}/, MODE_STRING).sub(/{jquery_selector}/, JQUERY_SELECTOR)

dest_path = File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
dest_file = File.new(dest_path, "w")
dest_file.write(source_text)
dest_file.close
end
dest_path = File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
dest_file = File.new(dest_path, "w")
dest_file.write(source_text)
dest_file.close
end

def self.rmdir_tree(root)
Dir.entries(root).each do |fname|
if fname[0] == '.'
next
else
path = File.join(root, fname)
if File.directory?(path)
rmdir_tree(path)
elsif File.file?(path)
File.delete(path)
def self.rmdir_tree(root)
Dir.entries(root).each do |fname|
if fname[0] == '.'
next
else
path = File.join(root, fname)
if File.directory?(path)
rmdir_tree(path)
elsif File.file?(path)
File.delete(path)
end
end
end
Dir.rmdir(root)
end
Dir.rmdir(root)
end
end

namespace :use_tinymce do
desc "uninstall use_tinymce javascript code"
task :uninstall do
init_file_path = File.join(UseTinyMCE::JAVASCRIPT_ROOT, 'use_tinymce_init.js')
init_file_path = File.join(UseTinyMCE::RakeSupport::JAVASCRIPT_ROOT, 'use_tinymce_init.js')
File.delete(init_file_path) if File.exists? init_file_path

tinymce_root_path = File.join(UseTinyMCE::JAVASCRIPT_ROOT, 'tinymce')
UseTinyMCE::rmdir_tree(tinymce_root_path) if File.exists? tinymce_root_path
tinymce_root_path = File.join(UseTinyMCE::RakeSupport::JAVASCRIPT_ROOT, 'tinymce')
UseTinyMCE::RakeSupport::rmdir_tree(tinymce_root_path) if File.exists? tinymce_root_path
end
task :install_tinymce_advanced => :uninstall do
FileUtils.cp_r File.join(UseTinyMCE::ASSETS_ROOT, 'tinymce_no_jquery', 'tinymce'), UseTinyMCE::JAVASCRIPT_ROOT
FileUtils.cp_r File.join(UseTinyMCE::RakeSupport::ASSETS_ROOT, 'tinymce_no_jquery', 'tinymce'), UseTinyMCE::RakeSupport::JAVASCRIPT_ROOT
end
task :install_tinymce_jquery => :uninstall do
FileUtils.cp_r File.join(UseTinyMCE::UseTinyMCE::ASSETS_ROOT, 'tinymce_jquery', 'tinymce'), UseTinyMCE::JAVASCRIPT_ROOT
FileUtils.cp_r File.join(UseTinyMCE::RakeSupport::UseTinyMCE::RakeSupport::ASSETS_ROOT, 'tinymce_jquery', 'tinymce'), UseTinyMCE::RakeSupport::JAVASCRIPT_ROOT
end

desc "Install tinymce with 'simple' initialization"
task :install_simple => :install_tinymce_advanced do
UseTinyMCE::copy_init_script('use_tinymce_init_simple.js')
# FileUtils.cp File.join(UseTinyMCE::ASSETS_ROOT, 'use_tinymce_init_simple.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
UseTinyMCE::RakeSupport::copy_init_script('use_tinymce_init_simple.js')
# FileUtils.cp File.join(UseTinyMCE::RakeSupport::ASSETS_ROOT, 'use_tinymce_init_simple.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
end

desc "Install tinymce with 'advanced' initialization"
task :install_advanced => :install_tinymce_advanced do
UseTinyMCE::copy_init_script('use_tinymce_init_advanced.js')
# FileUtils.cp File.join(UseTinyMCE::ASSETS_ROOT, 'use_tinymce_init_advanced.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
UseTinyMCE::RakeSupport::copy_init_script('use_tinymce_init_advanced.js')
# FileUtils.cp File.join(UseTinyMCE::RakeSupport::ASSETS_ROOT, 'use_tinymce_init_advanced.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
end

desc "Install tinymce jquery plugin with 'advanced' initialization"
task :install_jquery => :install_tinymce_jquery do
UseTinyMCE::copy_init_script('use_tinymce_init_jquery.js')
# FileUtils.cp File.join(UseTinyMCE::ASSETS_ROOT, 'use_tinymce_init_jquery.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
UseTinyMCE::RakeSupport::copy_init_script('use_tinymce_init_jquery.js')
# FileUtils.cp File.join(UseTinyMCE::RakeSupport::ASSETS_ROOT, 'use_tinymce_init_jquery.js'), File.join(JAVASCRIPT_ROOT, 'use_tinymce_init.js')
end
end
18 changes: 4 additions & 14 deletions lib/use_tinymce/use_tinymce.rb
Expand Up @@ -25,27 +25,17 @@ def mod.use_tinymce(*actions)

if defined? Rails
module Link
case
when Rails.version =~ /^3.0/
if Rails.version =~ /^3.[01]/
def use_tinymce_link
if defined?(params) && use_tinymce?(params[:action])
# javascript_include_tag( 'tinymce/jscripts/tiny_mce/jquery.tiny_mce', 'use_tinymce_init' )
jq_path = File.join(Rails.root, 'public', 'javascripts', 'tinymce', 'jscripts', 'tiny_mce', 'jquery.tinymce.js')
puts "Path to jquery.tinymce.js: #{jq_path}"
puts "Path Exists? #{File.exists?(jq_path)}"
jq_path = File.join(Rails.root, 'public', 'javascripts', 'tinymce', 'jscripts', 'tiny_mce', 'jquery.tinymce.js')
if File.exists? jq_path
javascript_include_tag( 'tinymce/jscripts/tiny_mce/jquery.tinymce', 'use_tinymce_init' )
javascript_include_tag( '/javascripts/tinymce/jscripts/tiny_mce/jquery.tinymce', '/javascripts/use_tinymce_init' )
else
javascript_include_tag( 'tinymce/jscripts/tiny_mce/tiny_mce', 'use_tinymce_init' )
javascript_include_tag( '/javascripts/tinymce/jscripts/tiny_mce/tiny_mce', '/javascripts/use_tinymce_init' )
end
end
end

when Rails.version =~ /^3.1/
def use_tinymce_link
# do nothing. Rails 3.1.x pulls in all the code in /app/assets/javascripts if you
# use javascript_include_tag "application"
end
else
logger.debug("use_tinymce has not been tested for this version of Rails: #{Rails.version}")
end
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion use_tinymce.gemspec
Expand Up @@ -12,5 +12,5 @@ Gem::Specification.new do |s|
s.files = Dir["{lib,test}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.markdown"] \
+ ["assets/use_tinymce_init_simple.js", "assets/use_tinymce_init_advanced.js", "assets/use_tinymce_init_jquery.js"] \
+ Dir['assets/tinymce*/**/*']
s.version = "0.0.8"
s.version = "0.0.9"
end

0 comments on commit 6d37145

Please sign in to comment.