almost-happy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Darcy Laycock
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Almost Happy #
2
+
3
+ Almost happy is a set of content management tools for ActiveRecord 3 and higher. It's built
4
+ on a simple, clean code base that makes it easy to do trivial content management tools.
5
+
6
+ ## Usage ##
7
+
8
+ First off, you'll need to add the gem to the Gemfile. For most cases, the following line should do:
9
+
10
+ gem "almost-happy"
11
+
12
+ Then, run `bundle install`.
13
+
14
+ Currently it provides two sets of functionality - publishable and convertable.
15
+
16
+ E.g. to make a model publishable:
17
+
18
+ class Page < ActiveRecord::Base
19
+ is_publishable
20
+ end
21
+
22
+ And, for each content field, declare it convertable:
23
+
24
+ class Page < ActiveRecord::Base
25
+ is_publishable
26
+ is_convertable :content
27
+ end
28
+
29
+ And before each save it will automatically convert the content to html
30
+ using the specific format etc.
31
+
32
+ ### is\_publishable ###
33
+
34
+ When a model is marked as publishable, it provides the following instance methods:
35
+
36
+ * `published?` - returns a boolean indicating if it's publishe
37
+ * `publish!` - mark the post as published right now.
38
+
39
+ And, the following class methods:
40
+
41
+ * `published` - returns a relation with all published posts
42
+ * `ordered` - returns a relation ordered by the published at time
43
+ * `ordered_for_preview` - returns a relation ordered by the published at time, those unpublished first.
44
+
45
+ ### is\_convertable ###
46
+
47
+ When a model is marked as convertable, it adds hooks (inc. validation) to convert
48
+ raw content in one format to another.
49
+
50
+ the `is_convertable` call accepts field names as the argument and an optional options hash.
51
+
52
+ The options hash currently has one options - `:validate` - defaulting to true, determining whether
53
+ the presence and format is validated.
54
+
55
+ For each field passed to `is_convertable`, it will provide the following methods:
56
+
57
+ * `#{field}_as_html` - rendered version as a html safe string
58
+
59
+ When using on a model, it expects a format (string) column exists and for each field the following:
60
+
61
+ * `#{field}` - stores the original, editable content (in whichever format is specified)
62
+ * `rendered_#{field}` - stores the converted field value
63
+
64
+ When rendering a form, please note you can also get a set of formats for a select field using
65
+ `AlmostHappy::Convertor.for_select`.
66
+
67
+ ## Note on Patches/Pull Requests ##
68
+
69
+ 1. Fork the project.
70
+ 2. Make your feature addition or bug fix.
71
+ 3. Add tests for it. This is important so I don't break it in a future version unintentionally.
72
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
73
+ 5. Send me a pull request. Bonus points for topic branches.
74
+
75
+ ## Copyright ##
76
+
77
+ Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
78
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'lib/almost_happy'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "almost-happy"
9
+ gem.summary = %Q{Simple tools for AR + Rails 3 to help make managing your content easy}
10
+ gem.description = %Q{Helpers, convertors etc for Rails 3 to make page / post models for simple cases simple}
11
+ gem.email = "sutto@sutto.net"
12
+ gem.homepage = "http://github.com/Sutto/almost-happy"
13
+ gem.authors = ["Darcy Laycock"]
14
+ gem.version = AlmostHappy::VERSION
15
+ gem.add_dependency "activerecord", ">= 3.0.0.beta3"
16
+ gem.add_dependency "RedCloth"
17
+ gem.add_dependency "rdiscount"
18
+ gem.add_dependency "nokogiri"
19
+ gem.add_development_dependency "shoulda", ">= 0"
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/test_*.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |test|
36
+ test.libs << 'test'
37
+ test.pattern = 'test/**/test_*.rb'
38
+ test.verbose = true
39
+ end
40
+ rescue LoadError
41
+ task :rcov do
42
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
43
+ end
44
+ end
45
+
46
+ task :test => :check_dependencies
47
+ task :default => :test
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{almost-happy}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Darcy Laycock"]
12
+ s.date = %q{2010-05-03}
13
+ s.description = %q{Helpers, convertors etc for Rails 3 to make page / post models for simple cases simple}
14
+ s.email = %q{sutto@sutto.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "almost-happy.gemspec",
26
+ "lib/almost-happy.rb",
27
+ "lib/almost_happy.rb",
28
+ "lib/almost_happy/active_record_mixin.rb",
29
+ "lib/almost_happy/convertable.rb",
30
+ "lib/almost_happy/convertor.rb",
31
+ "lib/almost_happy/format_validator.rb",
32
+ "lib/almost_happy/publishable.rb",
33
+ "lib/almost_happy/scopeable.rb",
34
+ "test/helper.rb",
35
+ "test/test_almost-happy.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/Sutto/almost-happy}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.6}
41
+ s.summary = %q{Simple tools for AR + Rails 3 to help make managing your content easy}
42
+ s.test_files = [
43
+ "test/helper.rb",
44
+ "test/test_almost-happy.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
53
+ s.add_runtime_dependency(%q<RedCloth>, [">= 0"])
54
+ s.add_runtime_dependency(%q<rdiscount>, [">= 0"])
55
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
56
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
59
+ s.add_dependency(%q<RedCloth>, [">= 0"])
60
+ s.add_dependency(%q<rdiscount>, [">= 0"])
61
+ s.add_dependency(%q<nokogiri>, [">= 0"])
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
66
+ s.add_dependency(%q<RedCloth>, [">= 0"])
67
+ s.add_dependency(%q<rdiscount>, [">= 0"])
68
+ s.add_dependency(%q<nokogiri>, [">= 0"])
69
+ s.add_dependency(%q<shoulda>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1 @@
1
+ require 'almost_happy'
@@ -0,0 +1,22 @@
1
+ require 'active_support'
2
+ require 'active_support/dependencies/autoload'
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'active_support/concern'
5
+
6
+ module AlmostHappy
7
+ VERSION = '0.1.0'.freeze
8
+
9
+ extend ActiveSupport::Autoload
10
+
11
+ autoload :ActiveRecordMixin
12
+
13
+ autoload :Convertor
14
+ autoload :FormatValidator
15
+
16
+ autoload :Convertable
17
+ autoload :Scopeable
18
+ autoload :Publishable
19
+
20
+ ::ActiveRecord::Base.send :include, ActiveRecordMixin if defined?(::ActiveRecord::Base)
21
+
22
+ end
@@ -0,0 +1,27 @@
1
+ module AlmostHappy
2
+ module ActiveRecordMixin
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def is_convertable(*args)
8
+ include AlmostHappy::Convertable
9
+ options = args.extract_options!
10
+ fields = args.map { |arg| arg.to_sym }.uniq
11
+ self.convertable_fields += fields
12
+ # Define accessors for each field.
13
+ fields.each do |field|
14
+ define_method(:"#{field}_as_html") { send(field).to_s.html_safe }
15
+ end
16
+ validate_convertable_format if options.fetch(:validate, true)
17
+ end
18
+
19
+ def is_publishable
20
+ include AlmostHappy::Publishable
21
+ include AlmostHappy::Scopeable
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require 'set'
2
+
3
+ module AlmostHappy
4
+ module Convertable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :convertable_fields
9
+ self.convertable_fields = Set.new
10
+ before_save :process_convertable_sections
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def validate_convertable_format
16
+ validates_presence_of :format
17
+ validates_with AlmostHappy::FormatValidator
18
+ end
19
+
20
+ end
21
+
22
+ module InstanceMethods
23
+
24
+ protected
25
+
26
+ def process_convertable_sections
27
+ convertable_fields.each do |field|
28
+ AlmostHappy::Convertor.new(self, field).render if should_render?(field)
29
+ end
30
+ end
31
+
32
+ def should_render?(field)
33
+ send(:"rendered_#{field}").blank? || format_changed? || send(:"#{field}_changed?")
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,93 @@
1
+ module AlmostHappy
2
+ class Convertor
3
+
4
+ class Renderer < Struct.new(:name, :options, :renderer)
5
+
6
+ def [](key)
7
+ options[key.to_sym]
8
+ end
9
+
10
+ def render(content)
11
+ renderer.call(content.to_s).to_s.html_safe
12
+ end
13
+
14
+ def human_name
15
+ ::I18n.t(name, :scope => :"almost_happy.renderers.human_names", :default => name.to_s.humanize)
16
+ end
17
+
18
+ end
19
+
20
+ class_attribute :renderers, :renderer_options
21
+ self.renderers = {}
22
+
23
+ def self.add_renderer(key, *args, &blk)
24
+ options = args.extract_options!
25
+ renderer = (args.pop || blk).to_proc
26
+ self.renderers[key.to_s] = Renderer.new(key, options, renderer)
27
+ end
28
+
29
+ def self.render_for(key, content)
30
+ renderer = self.renderers[key.to_s]
31
+ content = content.to_s
32
+ renderer.present? ? renderer.render(content) : content
33
+ end
34
+
35
+ def self.for_select
36
+ self.renderers.values.map do |r|
37
+ [r.human_name, r.name.to_s]
38
+ end.sort
39
+ end
40
+
41
+ def self.format_names
42
+ self.renderers.values.map { |r| r.human_name }.to_sentence :two_words_connector => " or ", :last_word_connector => ", or "
43
+ end
44
+
45
+ def self.[](key)
46
+ renderers[key.to_s].try(:options) || {}
47
+ end
48
+
49
+ def self.valid_format?(name)
50
+ renderers.keys.include? name.to_s
51
+ end
52
+
53
+ attr_reader :object, :source_field
54
+
55
+ def initialize(object, source_field)
56
+ @object = object
57
+ @source_field = source_field
58
+ end
59
+
60
+ def original_content
61
+ @object.send source_field
62
+ end
63
+
64
+ def rendered_content=(value)
65
+ @object.send :"rendered_#{source_field}=", value
66
+ end
67
+
68
+ def to_html
69
+ conversion_from = @object.format rescue nil
70
+ self.class.render_for(conversion_from, original_content)
71
+ end
72
+
73
+ def render
74
+ self.rendered_content = self.to_html
75
+ end
76
+
77
+ add_renderer :textile do |raw|
78
+ Albino.highlight_code RedCloth.new(raw).to_html
79
+ end
80
+
81
+ add_renderer :markdown, :filters => [:smart, :autolink] do |raw|
82
+ html = RDiscount.new(raw, *ContentRenderer[:markdown].fetch(:filters, [])).to_html
83
+ html.gsub!(/^(?:<p>)?@@@(?:<\/p>)?$/, '</div>')
84
+ html.gsub!(/^(?:<p>)?@@@\s*([\w\+]+)(?:<\/p>)?$/, '<div class="code" rel="\1">')
85
+ Albino.highlight_code(html)
86
+ end
87
+
88
+ add_renderer :raw do |raw|
89
+ Albino.highlight_code(raw.to_s)
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,16 @@
1
+ module AlmostHappy
2
+ class FormatValidator < ActiveModel::Validator
3
+
4
+ def validate(record)
5
+ if record && record.respond_to?(:format)
6
+ format = record.format
7
+ if format.present? && !AlmostHappy::Convertor.valid_format?(format)
8
+ record.errors.add :format, :invalid_format,
9
+ :default => 'is not a valid format (must be one of {{formats}})',
10
+ :formats => AlmostHappy::Convertor.format_names
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module AlmostHappy
2
+ module Publishable
3
+ extend ActiveSupport::Concern
4
+
5
+ module InstanceMethods
6
+
7
+ def published?
8
+ published_at.present? && published_at <= Time.now
9
+ end
10
+
11
+ def publish!
12
+ update_attribute :published_at, Time.now
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module AlmostHappy
2
+ module Scopeable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def published
8
+ where 'published_at IS NOT NULL AND published_at <= ?', Time.now
9
+ end
10
+
11
+ def ordered
12
+ order 'published_at DESC'
13
+ end
14
+
15
+ def ordered_for_preview
16
+ order '(published_at IS NULL) DESC, published_at DESC'
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'almost-happy'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAlmostHappy < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: almost-happy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Darcy Laycock
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-03 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta3
32
+ version: 3.0.0.beta3
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: RedCloth
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdiscount
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: nokogiri
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ type: :runtime
70
+ version_requirements: *id004
71
+ - !ruby/object:Gem::Dependency
72
+ name: shoulda
73
+ prerelease: false
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id005
83
+ description: Helpers, convertors etc for Rails 3 to make page / post models for simple cases simple
84
+ email: sutto@sutto.net
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE
91
+ - README.md
92
+ files:
93
+ - .document
94
+ - .gitignore
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - almost-happy.gemspec
99
+ - lib/almost-happy.rb
100
+ - lib/almost_happy.rb
101
+ - lib/almost_happy/active_record_mixin.rb
102
+ - lib/almost_happy/convertable.rb
103
+ - lib/almost_happy/convertor.rb
104
+ - lib/almost_happy/format_validator.rb
105
+ - lib/almost_happy/publishable.rb
106
+ - lib/almost_happy/scopeable.rb
107
+ - test/helper.rb
108
+ - test/test_almost-happy.rb
109
+ has_rdoc: true
110
+ homepage: http://github.com/Sutto/almost-happy
111
+ licenses: []
112
+
113
+ post_install_message:
114
+ rdoc_options:
115
+ - --charset=UTF-8
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project:
135
+ rubygems_version: 1.3.6
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Simple tools for AR + Rails 3 to help make managing your content easy
139
+ test_files:
140
+ - test/helper.rb
141
+ - test/test_almost-happy.rb