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

Returns a new instance of DraftEntry.



30
31
32
33
# File 'app/models/pageflow/draft_entry.rb', line 30

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



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

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

.find(id) ⇒ Object



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

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

.for_file_usage(usage) ⇒ Object



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

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

Instance Method Details

#create_file!(model, attributes) ⇒ Object



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

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.



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

def entry_title
  entry.title
end

#home_buttonObject



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

def home_button
  HomeButton.new(draft, theming)
end

#overview_buttonObject



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

def overview_button
  OverviewButton.new(draft)
end

#remove_file(file) ⇒ Object



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

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



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

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

#save!Object



70
71
72
# File 'app/models/pageflow/draft_entry.rb', line 70

def save!
  draft.save!
end

#stylesheet_cache_keyObject



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

def stylesheet_cache_key
  draft.cache_key
end

#stylesheet_modelObject



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

def stylesheet_model
  draft
end

#update_meta_data!(attributes) ⇒ Object



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

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

#use_file(file) ⇒ Object



65
66
67
68
# File 'app/models/pageflow/draft_entry.rb', line 65

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