Module: DynamicContent

Extended by:
ActiveSupport::Concern
Included in:
PagesController
Defined in:
app/controllers/concerns/dynamic_content.rb

Instance Method Summary collapse

Instance Method Details

#add_to_csv(mapper, row) ⇒ Object



113
114
115
116
117
# File 'app/controllers/concerns/dynamic_content.rb', line 113

def add_to_csv(mapper, row)
  CSV.open("#{Rails.root}/lib/mappers/#{mapper}.csv", "a+") do |csv|
    csv << row
  end
end

#csv_is_empty(mapper) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/concerns/dynamic_content.rb', line 119

def csv_is_empty(mapper)
  CSV.open("#{Rails.root}/lib/mappers/#{mapper}.csv", "r") do |csv|
    count = csv.readlines.size
    flag = true
    if count > 1
      flag = false
    end
    return flag
  end
end

#csv_to_json(mapper) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/concerns/dynamic_content.rb', line 130

def csv_to_json(mapper)
  col_data = Array.new
  first = true
  CSV.foreach("#{Rails.root}/lib/mappers/#{mapper}.csv") do |row|
    if first
      first = false
      next
    end
    col_data.append({
      :title => row[2],
      :route => row[1],
      :id => row[0],
      :redirect_to => row[3],
      :thumbnail => row[4]
    })
  end
  col_data
end

#exact(row_key, match_term) ⇒ Object



153
154
155
# File 'app/controllers/concerns/dynamic_content.rb', line 153

def exact(row_key, match_term)
  row_key == match_term
end

#find_match(csv, match_term, query) ⇒ Object



149
150
151
# File 'app/controllers/concerns/dynamic_content.rb', line 149

def find_match(csv, match_term, query)
  csv.detect {|row| send(query, row[:route].downcase, match_term.downcase)} || nil
end

#get_id_by_route(route, mapper) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/concerns/dynamic_content.rb', line 56

def get_id_by_route(route, mapper)
  data_list = csv_to_json(mapper)

  page_id = 0
  data_list.each do |data|
    page_id += 1
    if data[:route] == route
      break
    end
  end
  page_id
end

#get_last_id_from_csv(mapper) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/concerns/dynamic_content.rb', line 69

def get_last_id_from_csv(mapper)
  data_list = csv_to_json(mapper)

  if data_list.any?
    last_id = data_list[-1][:id].to_i
  else
    last_id = 0
  end
  (last_id + 1).to_s
end

#get_row_by_route(route, mapper) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/concerns/dynamic_content.rb', line 44

def get_row_by_route(route, mapper)
  data_list = csv_to_json(mapper)
  row = nil

  data_list.each do |data|
    if data[:route] == route
      row = data
    end
  end
  row
end

#parse_csv(mapper) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/concerns/dynamic_content.rb', line 14

def parse_csv(mapper)
  csv_path = "#{Rails.root}/lib/mappers/#{mapper}.csv"
  csv_dir = "#{Rails.root}/lib/mappers"
  Dir.mkdir(csv_dir) unless File.exists?(csv_dir)
  if (!File.exist?(csv_path))
    File.open(csv_path, "w+") do |f|
      f.write("id,route,title,redirect_to,thumbnail\n")
    end
  end
  CSV.parse(File.read(csv_path), :headers => true, :header_converters => :symbol)
end

#partial(row_key, match_term) ⇒ Object



157
158
159
# File 'app/controllers/concerns/dynamic_content.rb', line 157

def partial(row_key, match_term)
  match_term.include?(row_key)
end

#remove_by_route(route, mapper) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/concerns/dynamic_content.rb', line 80

def remove_by_route(route, mapper)
  table = CSV.table("#{Rails.root}/lib/mappers/#{mapper}.csv")

  page_id = get_id_by_route(route, mapper)

  html_path = "#{Rails.root}/app/views/pages/page-#{page_id}.html.erb"
  assets_dirs = [
      "#{Rails.root}/app/assets/images/page-#{page_id}",
      "#{Rails.root}/app/assets/stylesheets/page-#{page_id}",
      "#{Rails.root}/app/assets/javascripts/page-#{page_id}"
  ]

  File.delete(html_path) if File.exist?(html_path)

  for dir in assets_dirs
    FileUtils.remove_dir(dir) if File.directory?(dir)
  end

  table.delete_if do |row|
    row[:route] == route
  end

  if table.size() != 0
    File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
      f.write(table.to_csv(write_headers: true))
    end
  else
    File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
      f.write("id,route,title,redirect_to,thumbnail\n")
    end
  end
end

#set_dynamic_content(mapper, match_term, query = :exact) ⇒ Object



6
7
8
9
10
11
12
# File 'app/controllers/concerns/dynamic_content.rb', line 6

def set_dynamic_content(mapper, match_term, query = :exact)
  dynamic_content = Rails.cache.fetch("#{mapper}-#{match_term}-dynamic", :expires_in => 1.seconds) do
    match_row = find_match(parse_csv(mapper), match_term, query)
    match_row.to_h
  end
  @dynamic_content = OpenStruct.new(dynamic_content)
end

#update_csv_by_id(id, route, title, redirect, mapper) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/concerns/dynamic_content.rb', line 26

def update_csv_by_id(id, route, title, redirect, mapper)
  table = CSV.table("#{Rails.root}/lib/mappers/#{mapper}.csv")
  table.each do |row|
    if row[:id] == id.to_i
      if redirect and redirect.empty?
        redirect = nil
      end
      row[:redirect_to] = redirect
      row[:route] = route
      row[:title] = title
    end
  end

  File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
    f.write(table.to_csv(write_headers: true))
  end
end