Class: Scaffolding::SuperchartsChartTransformer

Inherits:
SuperchartsTransformer
  • Object
show all
Defined in:
lib/scaffolding/supercharts_chart_transformer.rb

Constant Summary collapse

RUBY_NEW_CHARTS_HOOK =
"# 📈 super scaffolding will insert new charts above this line."

Instance Method Summary collapse

Instance Method Details

#parent_show_fileObject



121
122
123
# File 'lib/scaffolding/supercharts_chart_transformer.rb', line 121

def parent_show_file
  @target_show_file ||= "./app/views/account/scaffolding/absolutely_abstract/creative_concepts/show.html.erb"
end

#replacement_for(string) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/scaffolding/supercharts_chart_transformer.rb', line 139

def replacement_for(string)
  case string
  when "Scaffolding::CompletelyConcrete::TangibleThings::TangibleThingsChart"
    child.pluralize + "::" + child.pluralize + "Chart"
  when "lib/scaffolding/app"
    "app"
  else
    "🛑"
  end
end

#scaffold_superchartsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
112
113
114
115
116
117
118
119
# File 'lib/scaffolding/supercharts_chart_transformer.rb', line 6

def scaffold_supercharts
  super

  # copy files over and do the appropriate string replace.
  files = [
    "./lib/scaffolding/app/controllers/account/scaffolding/completely_concrete/tangible_things/tangible_things_chart_controller.rb",
    "./app/views/account/scaffolding/completely_concrete/tangible_things/tangible_things_chart",
    "./app/views/shared/supercharts"
  ].compact

  files.each do |name|
    if File.directory?(resolve_template_path(name))
      scaffold_directory(name)
    else
      scaffold_file(name)
    end
  end

  # locale
  locale_file = "./config/locales/en/scaffolding/completely_concrete/tangible_things.en.yml"

  # add locale file if missing
  unless File.file?(transform_string(locale_file))
    scaffold_file(locale_file)
    add_locale_helper_export_fix
  end

  # add locale strings for chart
  scaffold_add_line_to_file(locale_file, RUBY_NEW_CHARTS_HOOK, "  account:", prepend: true, increase_indent: true, exact_match: true)
  # ensure the right indentation
  scaffold_replace_line_in_file(locale_file, "    #{RUBY_NEW_CHARTS_HOOK}", "  #{RUBY_NEW_CHARTS_HOOK}")

  locale_yaml = "    chart:\n      filters:\n        1w: \n          abbr: 1w\n          label: \"last week\"\n        1m:\n          abbr: 1m\n          label: \"last month\"\n        ytd:\n          abbr: ytd\n          label: \"year to date\"\n      description:\n        1w: Tangible Things last 7 days\n        1m: Tangible Things last month\n        ytd: Tangible Things since start of year\n      contextual_description:\n        1w: Tangible Things on <span class=\"whitespace-nowrap\">%label%</span>\n        1m: Tangible Things in <span class=\"whitespace-nowrap\">%label%</span>\n        ytd: Tangible Things in <span class=\"whitespace-nowrap\">%label%</span>\n      alt_description:\n        1w: Chart of Tangible Things last 7 days\n        1m: Chart of Tangible Things last month\n        ytd: Chart of Tangible Things since start of year\n      date_abbr:\n        day: \"%e\"\n        week: \"Week of %b %e\"\n        month: \"%b\"\n      date_full:\n        day: \"%B %e\"\n        week: \"week of %B %e\"\n        month: \"%B, %Y\"\n  YAML\n\n  scaffold_add_line_to_file(\"./config/locales/en/scaffolding/completely_concrete/tangible_things.en.yml\", locale_yaml, RUBY_NEW_CHARTS_HOOK, prepend: true)\n\n  # add children to the show page of their parent.\n  unless cli_options[\"skip-parent\"] || parent == \"None\"\n    lines_to_add = <<~RUBY\n      <div class=\"mt-4 [--chart-height:150px] md:[--chart-height:200px]\">\n        <%= turbo_frame_tag :charts_tangible_things, src: polymorphic_path([:account, @creative_concept, :tangible_things, :chart], timespan: \"1m\") do %>\n          <%= render \"shared/supercharts/chart_skeleton\" do %>\n            <%= t('tangible_things.label') %>&hellip;\n          <% end %>\n        <% end %>\n      </div>\n    RUBY\n    scaffold_add_line_to_file(\n      parent_show_file,\n      lines_to_add,\n      \"<%# \u{1F685} super scaffolding will insert new children above this line. %>\",\n      prepend: true\n    )\n  end\n\n  # add user permissions.\n  add_ability_line_to_roles_yml\n\n  # apply routes.\n  # TODO this is a hack and should be in its own RouteFileManipulator class\n  lines = File.read(\"config/routes.rb\").lines.map(&:chomp)\n  account_namespace_found = false\n\n  lines.each_with_index do |line, index|\n    if line.match?(\"namespace :account do\")\n      account_namespace_found = true\n    elsif account_namespace_found && line.match?(transform_string(\"resources :tangible_things\"))\n      chart_resource_lines = transform_string(\"collection do\\nresource :chart, only: :show, module: :tangible_things, as: :tangible_things_chart, controller: :tangible_things_chart\\nend\")\n      lines[index] = if line.match?(/do$/)\n        \"\#{line}\\n\#{chart_resource_lines}\\n\"\n      else\n        \"\#{line} do\\n\#{chart_resource_lines}\\nend\"\n      end\n    end\n  end\n\n  File.write(\"config/routes.rb\", lines.join(\"\\n\"))\n\n  puts `standardrb --fix ./config/routes.rb`\n\n  restart_server unless ENV[\"CI\"].present?\nend\n"

#transform_string(string) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/scaffolding/supercharts_chart_transformer.rb', line 125

def transform_string(string)
  [
    "Scaffolding::CompletelyConcrete::TangibleThings::TangibleThingsChart",
    "lib/scaffolding/app"
  ].each do |needle|
    # TODO There might be more to do here?
    # What method is this calling?
    string = string.gsub(needle, encode_double_replacement_fix(replacement_for(needle)))
  end

  string = super(string)
  decode_double_replacement_fix(string)
end