Class: SidebarSection

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/sidebar_section.rb

Constant Summary collapse

MAX_TITLE_LENGTH =
30

Instance Method Summary collapse

Instance Method Details

#reset_community!Object



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
# File 'app/models/sidebar_section.rb', line 32

def reset_community!
  ActiveRecord::Base.transaction do
    self.update!(title: "Community")
    self.sidebar_section_links.destroy_all
    community_urls =
      SidebarUrl::COMMUNITY_SECTION_LINKS.map do |url_data|
        "('#{url_data[:name]}', '#{url_data[:path]}', '#{url_data[:icon]}', '#{url_data[:segment]}', false, now(), now())"
      end

    result = DB.query <<~SQL
    INSERT INTO sidebar_urls(name, value, icon, segment, external, created_at, updated_at)
    VALUES #{community_urls.join(",")}
    RETURNING sidebar_urls.id
    SQL

    sidebar_section_links =
      result.map.with_index do |url, index|
        "(-1, #{url.id}, 'SidebarUrl', #{self.id}, #{index},  now(), now())"
      end

    DB.query <<~SQL
    INSERT INTO sidebar_section_links(user_id, linkable_id, linkable_type, sidebar_section_id, position, created_at, updated_at)
    VALUES #{sidebar_section_links.join(",")}
    SQL
  end
end