Class: ImportExport::BaseExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/import_export/base_exporter.rb

Constant Summary collapse

CATEGORY_ATTRS =
%i[
  id
  name
  color
  created_at
  user_id
  slug
  description
  text_color
  auto_close_hours
  position
  parent_category_id
  auto_close_based_on_last_post
  topic_template
  all_topics_wiki
  permissions_params
]
GROUP_ATTRS =
%i[
  id
  name
  created_at
  automatic_membership_email_domains
  primary_group
  title
  grant_trust_level
  incoming_email
  bio_raw
  allow_membership_requests
  full_name
  default_notification_level
  visibility_level
  public_exit
  public_admission
  membership_request_template
  messageable_level
  mentionable_level
  members_visibility_level
  publish_read_state
]
USER_ATTRS =
%i[
  id
  email
  username
  name
  created_at
  trust_level
  active
  last_emailed_at
  custom_fields
]
TOPIC_ATTRS =
%i[id title created_at views category_id closed archived archetype]
POST_ATTRS =
%i[
  id
  user_id
  post_number
  raw
  created_at
  reply_to_post_number
  hidden
  hidden_reason_id
  wiki
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



5
6
7
# File 'lib/import_export/base_exporter.rb', line 5

def categories
  @categories
end

#export_dataObject (readonly)

Returns the value of attribute export_data.



5
6
7
# File 'lib/import_export/base_exporter.rb', line 5

def export_data
  @export_data
end

Instance Method Details

#default_filename_prefixObject



251
252
253
# File 'lib/import_export/base_exporter.rb', line 251

def default_filename_prefix
  raise "Overwrite me!"
end

#export_categoriesObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/import_export/base_exporter.rb', line 78

def export_categories
  data = []

  categories.each do |cat|
    data << CATEGORY_ATTRS.inject({}) do |h, a|
      h[a] = cat.public_send(a)
      h
    end
  end

  data
end

#export_categories!Object



91
92
93
94
95
# File 'lib/import_export/base_exporter.rb', line 91

def export_categories!
  @export_data[:categories] = export_categories

  self
end

#export_category_groupsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/import_export/base_exporter.rb', line 121

def export_category_groups
  groups = []
  group_names = []
  auto_group_names = Group::AUTO_GROUPS.keys.map(&:to_s)

  @export_data[:categories].each do |c|
    c[:permissions_params].each do |group_name, _|
      group_names << group_name unless auto_group_names.include?(group_name.to_s)
    end
  end

  group_names.uniq!
  return [] if group_names.empty?

  export_groups(group_names)
end

#export_category_groups!Object



138
139
140
141
142
# File 'lib/import_export/base_exporter.rb', line 138

def export_category_groups!
  @export_data[:groups] = export_category_groups

  self
end

#export_group_usersObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/import_export/base_exporter.rb', line 144

def export_group_users
  user_ids = []

  @export_data[:groups].each { |g| user_ids += g[:user_ids] }

  user_ids.uniq!
  return User.none if user_ids.empty?

  users = User.where(id: user_ids)
  export_users(users)
end

#export_group_users!Object



156
157
158
159
160
# File 'lib/import_export/base_exporter.rb', line 156

def export_group_users!
  @export_data[:users] = export_group_users

  self
end

#export_groups(group_names) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/import_export/base_exporter.rb', line 97

def export_groups(group_names)
  data = []
  groups = Group.all
  groups = groups.where(name: group_names) if group_names.present?

  groups.find_each do |group|
    attrs =
      GROUP_ATTRS.inject({}) do |h, a|
        h[a] = group.public_send(a)
        h
      end
    attrs[:user_ids] = group.users.pluck(:id)
    data << attrs
  end

  data
end

#export_groups!Object



115
116
117
118
119
# File 'lib/import_export/base_exporter.rb', line 115

def export_groups!
  @export_data[:groups] = export_groups([])

  self
end

#export_topic_usersObject



203
204
205
206
207
208
209
210
# File 'lib/import_export/base_exporter.rb', line 203

def export_topic_users
  return if @export_data[:topics].blank?
  topic_ids = @export_data[:topics].pluck(:id)

  users = User.joins(:posts).where("posts.topic_id IN (?)", topic_ids).distinct

  export_users(users)
end

#export_topic_users!Object



212
213
214
215
216
# File 'lib/import_export/base_exporter.rb', line 212

def export_topic_users!
  @export_data[:users] = export_topic_users

  self
end

#export_topicsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/import_export/base_exporter.rb', line 162

def export_topics
  data = []

  @topics.each do |topic|
    puts topic.title

    topic_data =
      TOPIC_ATTRS.inject({}) do |h, a|
        h[a] = topic.public_send(a)
        h
      end

    topic_data[:posts] = []

    topic.ordered_posts.find_each do |post|
      attributes =
        POST_ATTRS.inject({}) do |h, a|
          h[a] = post.public_send(a)
          h
        end

      attributes[:raw] = attributes[:raw].gsub(
        'src="/uploads',
        "src=\"#{Discourse.base_url_no_prefix}/uploads",
      )

      topic_data[:posts] << attributes
    end

    data << topic_data
  end

  data
end

#export_topics!Object



197
198
199
200
201
# File 'lib/import_export/base_exporter.rb', line 197

def export_topics!
  @export_data[:topics] = export_topics

  self
end

#export_translation_overridesObject



241
242
243
244
245
246
247
248
249
# File 'lib/import_export/base_exporter.rb', line 241

def export_translation_overrides
  @export_data[:translation_overrides] = TranslationOverride.all.select(
    :locale,
    :translation_key,
    :value,
  )

  self
end

#export_users(users) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/import_export/base_exporter.rb', line 218

def export_users(users)
  data = []

  users.find_each do |u|
    next if u.id == Discourse::SYSTEM_USER_ID

    x =
      USER_ATTRS.inject({}) do |h, a|
        h[a] = u.public_send(a)
        h
      end

    x.merge(
      bio_raw: u..bio_raw,
      website: u..website,
      location: u..location,
    )
    data << x
  end

  data
end

#save_to_file(filename = nil) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/import_export/base_exporter.rb', line 255

def save_to_file(filename = nil)
  output_basename =
    filename ||
      File.join("#{default_filename_prefix}-#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.json")
  File.open(output_basename, "w:UTF-8") { |f| f.write(@export_data.to_json) }
  puts "Export saved to #{output_basename}"
  output_basename
end