diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-07-19 17:44:27 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-07-19 17:44:27 -0400 |
commit | 28ea5cdfd9676ba090eb84a77933559bd4a427a0 (patch) | |
tree | f9d4b3cf87195e14a909f271153c00e0497d87da /src | |
parent | 356016b39f901d63f02bf3ff31a1c3c638a822e8 (diff) | |
download | guff-28ea5cdfd9676ba090eb84a77933559bd4a427a0.tar.bz2 guff-28ea5cdfd9676ba090eb84a77933559bd4a427a0.zip |
clean up theme model
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/models/theme.cr | 116 |
1 files changed, 78 insertions, 38 deletions
diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr index a8682f7..61debce 100644 --- a/src/guff/models/theme.cr +++ b/src/guff/models/theme.cr @@ -19,19 +19,6 @@ class Guff::Models::ThemeModel < Guff::Models::Model ORDER BY LOWER(a.theme_name), a.theme_date ", - get_data: " - SELECT a.theme_id, - a.data_key, - a.data_val - - FROM theme_data a - JOIN theme_data_types b - ON (b.type_id = a.type_id) - - WHERE b.name = ? - AND a.theme_id IN (%s) - ", - get: " SELECT a.theme_id, a.theme_name, @@ -45,12 +32,34 @@ class Guff::Models::ThemeModel < Guff::Models::Model (SELECT COUNT(*) FROM pages WHERE theme_id = a.theme_id) AS num_pages + FROM themes a - ORDER BY LOWER(a.theme_name), a.theme_date + WHERE theme_id = ? + ", + + get_data: " + SELECT a.data_key, + a.data_val + + FROM theme_data a + JOIN theme_data_types b + ON (b.type_id = a.type_id) + + WHERE a.theme_id = ? + AND b.name = ? ", } + def initialize(context : Context) + super(context) + + @cache = { + templates: {} of Int32 => Hash(String, String), + metadata: {} of Int32 => Hash(String, String), + } + end + def all(with_metadata : Bool = false) # get rows rows = [] of Hash(String, String) @@ -62,38 +71,69 @@ class Guff::Models::ThemeModel < Guff::Models::Model end end - if with_metadata - # get metadata lut - lut = get_data("metadata", rows.map { |row| row["theme_id"].to_i }) + # return results + rows + end + + def get(theme_id : Int32) + @context.dbs.ro.row(SQL[:get], [theme_id.to_s]).not_nil! + end + + def templates(theme_id : Int32) + unless @cache[:templates].includes?(theme_id) + # get theme + row = get(theme_id) + + # load templates + @cache[:templates][theme_id] = (if row["is_system"] == "1" + # read templates from templates dir + read_templates(File.join( + @context.system_dir, + "themes", + row["theme_slug"], + "templates" + )) + else + get_data(theme_id, "templates") + end) + end + + # return cached results + @cache[:templates][theme_id] + end - # build and return result - rows.map { |row| - lut[row["theme_id"]]? ? row.merge(lut[row["theme_id"]]) : row - } - else - rows + def metadata(theme_id : Int32) + unless @cache[:metadata].includes?(theme_id) + # get theme + row = get(theme_id) + + # load templates + @cache[:metadata][theme_id] = (if row["is_system"] == "1" + # system themes have no metadata (for now) + {} of String => String + else + get_data(theme_id, "metadata") + end) end + + # return cached results + @cache[:metadata][theme_id] end - def get(theme_id : Int32) - id = theme_id.to_s - r = @context.dbs.ro.row(SQL[:get], [id]).not_nil! - lut = get_data("metadata", [theme_id]) - lut[id]? ? r.merge(lut[id]) : r + private def read_templates(dir_path : String) : Hash(String, String) + Dir.glob(File.join(templates_dir, "**/*")).reduce( + {} of String => String + ) do |r, path| + r[path.gsub(templates_dir + "/", "")] = File.read(path) + r + end end - def get_data(type : String, theme_ids : Array(Int32)) + private def get_data(theme_id : Int32, type : String) r = {} of String => Hash(String, String) - if theme_ids.size > 0 - @context.dbs.ro.all(SQL[:get_data] % [ - (["?"] * theme_ids.size).join(",") - ], [type].concat(theme_ids.map { |id| id.to_s })) do |row| - r[row["theme_id"].to_s] ||= {} of String => String - r[row["theme_id"].to_s][ - "%s/%s" % [type, row["data_key"].to_s] - ] = row["data_val"].to_s - end + @context.dbs.ro.all(SQL[:get_data], [theme_id.to_s, type]) do |row| + r[row["data_key"].to_s] = row["data_val"].to_s end # return results |