Created
October 4, 2009 19:48
-
-
Save gumayunov/201598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
result = @article.content.append_fragment params[:content] | |
@article.save | |
respond_to do |format| | |
format.json do | |
render :json => result | |
end | |
end | |
end | |
#------------------------- | |
class FragmentableTextile | |
def update_fragment(id, textile) | |
result = UpdateResult.empty | |
if idx = fragments.map(&:id).index(id) | |
parts = fragments.map(&:to_s) | |
parts[idx] = textile | |
new_fragments = parse_textile(parts.join "\n\n") | |
fragments = @fragments.dup | |
result = merge(fragments, new_fragments, idx) | |
@fragments = fragments | |
end | |
result | |
end | |
def append_fragment(textile) | |
result = UpdateResult.empty | |
new_fragments = parse_textile([fragments.to_s, textile].join "\n\n") | |
if new_fragments.size > @fragments.size | |
result.insert_set << { @fragments.last.id => new_fragments.last } | |
@fragments = new_fragments | |
end | |
result | |
end | |
#------------------------- | |
class Article < ActiveRecord::Base | |
fragmentable_textile_fields :content | |
plain_textile_fields :abstract | |
#------------------------- | |
module FragmentableTextileExtension | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
module ClassMethods | |
def fragmentable_textile_fields(*attributes) | |
attributes.each do |attribute| | |
serialize attribute, FragmentableTextile | |
define_method "#{attribute}=" do |value| | |
raise "direct_change of '#{attribute}' is not implemented" | |
end | |
end | |
end | |
end | |
end | |
module PlainTextileExtension | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
module ClassMethods | |
def plain_textile_fields(*attributes) | |
attributes.each do |attribute| | |
serialize attribute, PlainTextile | |
define_method "#{attribute}=" do |value| | |
self[attribute] = case value | |
when PlainTextile | |
value | |
else | |
PlainTextile.new value | |
end | |
end | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment