Class: Pageflow::DraftEntry

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion
Defined in:
app/models/pageflow/draft_entry.rb

Defined Under Namespace

Classes: InvalidForeignKeyCustomAttributeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, draft = nil) ⇒ DraftEntry

Returns a new instance of DraftEntry.



34
35
36
37
# File 'app/models/pageflow/draft_entry.rb', line 34

def initialize(entry, draft = nil)
  @entry = entry
  @draft = draft || entry.draft
end

Instance Attribute Details

#draftObject (readonly) Also known as: revision

Returns the value of attribute draft.



7
8
9
# File 'app/models/pageflow/draft_entry.rb', line 7

def draft
  @draft
end

#entryObject (readonly)

Returns the value of attribute entry.



7
8
9
# File 'app/models/pageflow/draft_entry.rb', line 7

def entry
  @entry
end

Class Method Details

.accessible_by(ability, action) ⇒ Object



95
96
97
98
99
# File 'app/models/pageflow/draft_entry.rb', line 95

def self.accessible_by(ability, action)
  Entry.includes(:draft).accessible_by(ability, action).map do |entry|
    DraftEntry.new(entry)
  end
end

.find(id) ⇒ Object



87
88
89
# File 'app/models/pageflow/draft_entry.rb', line 87

def self.find(id)
  new(Entry.find(id))
end

.for_file_usage(usage) ⇒ Object



91
92
93
# File 'app/models/pageflow/draft_entry.rb', line 91

def self.for_file_usage(usage)
  new(usage.revision.entry)
end

Instance Method Details

#create_file!(file_type, attributes) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/pageflow/draft_entry.rb', line 46

def create_file!(file_type, attributes)
  check_foreign_key_custom_attributes(file_type.custom_attributes, attributes)

  file = file_type.model.create!(attributes.except(:configuration)) do |f|
    f.entry = entry
  end

  usage = @draft.file_usages.create_with_lock!(file: file,
                                               configuration: attributes[:configuration])
  UsedFile.new(file, usage)
end

#emphasize_chapter_beginningObject



121
122
123
# File 'app/models/pageflow/draft_entry.rb', line 121

def emphasize_chapter_beginning
  revision.configuration['emphasize_chapter_beginning']
end

#emphasize_new_pagesObject



125
126
127
# File 'app/models/pageflow/draft_entry.rb', line 125

def emphasize_new_pages
  revision.configuration['emphasize_new_pages']
end

#entry_titleObject

So we can always get to the original Entry title.



42
43
44
# File 'app/models/pageflow/draft_entry.rb', line 42

def entry_title
  entry.title
end

#home_buttonObject



109
110
111
# File 'app/models/pageflow/draft_entry.rb', line 109

def home_button
  HomeButton.new(draft, theming)
end

#manual_startObject



117
118
119
# File 'app/models/pageflow/draft_entry.rb', line 117

def manual_start
  revision.configuration['manual_start']
end

#overview_buttonObject



113
114
115
# File 'app/models/pageflow/draft_entry.rb', line 113

def overview_button
  OverviewButton.new(draft)
end

#remove_file(file) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/pageflow/draft_entry.rb', line 58

def remove_file(file)
  draft.file_usages.where(file: file).destroy_all

  file.file_type.nested_file_types.each do |nested_file_type|
    nested_file_ids = file.nested_files(nested_file_type.model).map(&:id)

    draft
      .file_usages
      .where(file_type: nested_file_type.model.name,
             file_id: nested_file_ids)
      .destroy_all
  end

  file.destroy if file.usages.reload.empty?
end

#resolve_widgets(options = {}) ⇒ Object



129
130
131
# File 'app/models/pageflow/draft_entry.rb', line 129

def resolve_widgets(options = {})
  widgets.resolve(Pageflow.config_for(entry), options)
end

#save!Object



79
80
81
# File 'app/models/pageflow/draft_entry.rb', line 79

def save!
  draft.save!
end

#stylesheet_cache_keyObject



105
106
107
# File 'app/models/pageflow/draft_entry.rb', line 105

def stylesheet_cache_key
  draft.cache_key
end

#stylesheet_modelObject



101
102
103
# File 'app/models/pageflow/draft_entry.rb', line 101

def stylesheet_model
  draft
end

#update_meta_data!(attributes) ⇒ Object



83
84
85
# File 'app/models/pageflow/draft_entry.rb', line 83

def update_meta_data!(attributes)
  draft.update_attributes!(attributes)
end

#use_file(file) ⇒ Object



74
75
76
77
# File 'app/models/pageflow/draft_entry.rb', line 74

def use_file(file)
  draft.file_usages.create!(file: file.to_model,
                            configuration: file.configuration)
end