Module: LoyalCore::ActsAsContentDepartAble::ClassMethods

Defined in:
lib/loyal_core/acts/content_depart_able.rb

Defined Under Namespace

Modules: ContentOwnerInstanceMethods, ContentWorkerMethods

Instance Method Summary collapse

Instance Method Details

#loyal_core_acts_as_conent_worker(*args) ⇒ Object

options:

- owner_class


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/loyal_core/acts/content_depart_able.rb', line 83

def loyal_core_acts_as_conent_worker *args
  options =  args.extract_options!

  belongs_to :target, :polymorphic => true

  before_validation do |r|
    r.words_amount = r.content_text.size
  end

  include ContentWorkerMethods
end

#loyal_core_acts_as_content_owner(*args) ⇒ Object

options:

- worker_class


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/loyal_core/acts/content_depart_able.rb', line 18

def loyal_core_acts_as_content_owner *args
  options =  args.extract_options!

  self.attr_accessible :content, :outline, :content_code, :content_mode, :content_mode_was_cache

  worker_class = options[:worker_class]

  belongs_to :content_worker, :class_name => "#{worker_class.to_s}", :dependent => :destroy,
    :foreign_key => options[:foreign_key]

  before_validation do |r|
    # FIXME: 大纲该如何生成呢?

    r.outline = ::Sanitize.clean(
      r.content_text,
      ::LoyalCore.config.sanitize_config[:text]
    ).to_s[0...LOYAL_CORE_OUTLINE_MAX_LENGTH]
  end

  # validates_length_of :outline, :maximum => LOYAL_CORE_OUTLINE_MAX_LENGTH

  after_save do |r|
    r.lastest_content_worker.target = r
    r.lastest_content_worker.save!
  end

  define_method :lastest_content_worker do
    self.content_worker ||= worker_class.new
  end

  ###############################################
  define_singleton_method :content_mode_config do
    worker_class.content_mode_config
  end

  delegate :content=, :content, :content_text, :words_amount, :words_amount_more?,
    :content_code=, :content_code,
    :content_mode, :content_mode=,

    :content_mode_was_cache, :content_mode_was_cache=,
    :content_mode_was_cache_changed?,
    :content_mode_was_cache_reset!,

    :content_mode_name=, :content_mode_name,
    :to => :lastest_content_worker

  validate do |r|
    # 如果正文的格式改变了,需要提示
    if r.content_mode_was_cache_changed?
      r.errors.add(:content_mode, '变化了, 请检查正文格式') 
      r.content_mode_was_cache_reset!
    end
  end

  include ContentOwnerInstanceMethods
end