Class: Pageflow::DraftEntry

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, draft = nil) ⇒ DraftEntry



28
29
30
31
# File 'app/models/pageflow/draft_entry.rb', line 28

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

Instance Attribute Details

#draftObject (readonly)

Returns the value of attribute draft.



5
6
7
# File 'app/models/pageflow/draft_entry.rb', line 5

def draft
  @draft
end

#entryObject (readonly)

Returns the value of attribute entry.



5
6
7
# File 'app/models/pageflow/draft_entry.rb', line 5

def entry
  @entry
end

Class Method Details

.accessible_by(ability, action) ⇒ Object



84
85
86
87
88
# File 'app/models/pageflow/draft_entry.rb', line 84

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

.find(id) ⇒ Object



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

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

.for_file_usage(usage) ⇒ Object



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

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

Instance Method Details

#create_file(model, attributes) ⇒ Object



38
39
40
41
42
43
44
45
# File 'app/models/pageflow/draft_entry.rb', line 38

def create_file(model, attributes)
  file = model.create(attributes.except(:configuration)) do |f|
    f.entry = entry
  end

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

#entry_titleObject

So we can always get to the original Entry title.



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

def entry_title
  entry.title
end

#home_buttonObject



98
99
100
# File 'app/models/pageflow/draft_entry.rb', line 98

def home_button
  HomeButton.new(draft, theming)
end

#overview_buttonObject



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

def overview_button
  OverviewButton.new(draft)
end

#remove_file(file) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/pageflow/draft_entry.rb', line 47

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.empty?
end

#resolve_widgets(options = {}) ⇒ Object



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

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

#save!Object



68
69
70
# File 'app/models/pageflow/draft_entry.rb', line 68

def save!
  draft.save!
end

#stylesheet_cache_keyObject



94
95
96
# File 'app/models/pageflow/draft_entry.rb', line 94

def stylesheet_cache_key
  draft.cache_key
end

#stylesheet_modelObject



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

def stylesheet_model
  draft
end

#update_meta_data!(attributes) ⇒ Object



72
73
74
# File 'app/models/pageflow/draft_entry.rb', line 72

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

#use_file(file) ⇒ Object



63
64
65
66
# File 'app/models/pageflow/draft_entry.rb', line 63

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