Class: AvoUpgrade::Upgrade29to30

Inherits:
UpgradeTool show all
Defined in:
lib/avo_upgrade/upgrade29to30.rb

Instance Method Summary collapse

Methods inherited from UpgradeTool

#avo_global_files, #class_names_for, #enter_to_continue, #files_from, #remove_block_arg_on, #remove_config, #remove_text_on, #replace_in_filename, #replace_text_on, run

Instance Method Details

#runObject



3
4
5
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
# File 'lib/avo_upgrade/upgrade29to30.rb', line 3

def run
  # Replace all resource files with the new ones without the _resource suffix
  replace_in_filename "_resource", "", path: resources_path

  # Replace all "Avo::Dashboards::" with "AvoDashboards::" in all files
  # replace_text_on(avo_global_files, { "Avo::Dashboards::" => "Avo::Dashboards::" }, exact_match: false)

  replace_text_on(
    avo_global_files,
    {
      "Avo::Dashboards::BaseCard" => "Avo::Cards::BaseCard",
      "Avo::Dashboards::BaseDivider" => "Avo::Cards::BaseDivider",
      "Avo::Dashboards::ChartkickCard" => "Avo::Cards::ChartkickCard",
      "Avo::Dashboards::MetricCard" => "Avo::Cards::MetricCard",
      "Avo::Dashboards::PartialCard" => "Avo::Cards::PartialCard",
    },
    exact_match: false
  )

  # Create a hash with old class names as keys and new class names as values
  # Example: { "Avo::Resources::UserResource" => "Avo::Resources::User" }
  old_text_new_text_hash = class_names_for(:resources).map do |class_name|
    [class_name, "Avo::Resources::#{class_name.sub(/Resource$/, '')}"]
  end.to_h

  # Same as above but for all components, class name don't change, only the namespace
  [:actions, :filters, :resource_tools, :dashboards, :cards].each do |component|
    old_text_new_text_hash.merge! change_class_name_hash_for(component)
  end

  replace_text_on(files_from(actions_path), {"model" => "record"}, exact_match: false)
  replace_text_on(
    files_from(resources_path),
    {
      "model." => "record.",
      "model_class." => "query.",
      "scope." => "query.",
      "resolve_query_scope" => "index_query",
    },
    exact_match: false
  )
  replace_text_on(avo_global_files, old_text_new_text_hash)

  # Remove arguments from the blocks
  remove_text = [
    "(resource:)",
    "(model_class:, id:, params:)",
    "(model_class:)",
    "(value)",
    "|model|",
    "|model, resource|",
    "|model, resource, view|",
    "|model, resource, view, field|",
    "(model:, resource:, view:, field:)",
    "|model, &args|"
  ]
  remove_block_arg_on(files_from(resources_path) + files_from(actions_path) + files_from(filters_path), remove_text)

  remove_config("config.tabs_style")

  print "\n\nUpgrade to Avo 3.0 completed! 🚀\n\n"
end

#summaryObject



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
120
# File 'lib/avo_upgrade/upgrade29to30.rb', line 66

def summary
  # Get the names of all the resources, actions, filters, resource tools, dashboards and cards
  resources_names = class_names_for(:resources)
  actions_names = class_names_for(:actions)
  filters_names = class_names_for(:filters)
  resource_tools_names = class_names_for(:resource_tools)
  dashboards_names = class_names_for(:dashboards)
  cards_names = class_names_for(:cards)

  # Print a summary of the upgrade process
  puts "\n\nSummary of changes:\n" +
    "---------------------\n" +
    # "Renaming Avo::Dashboards:: to AvoDashboards::\n" +
    "Renaming resources naming from ClassNameResource to Avo::Resources::ClassName\n" +
    "Renaming actions naming from ClassName to Avo::Actions::ClassName\n" +
    "Renaming filters naming from ClassName to Avo::Filters::ClassName\n" +
    "Renaming resource tools naming from ClassName to Avo::ResourceTools::ClassName\n" +
    "Renaming dashboards naming from ClassName to Avo::Dashboards::ClassName\n" +
    "Renaming cards naming from ClassName to Avo::Cards::ClassName\n" +
    "Changing cards namespace from Dashboards to Cards\n" +
    "Renaming 'resolve_query_scope' method in resource files to 'index_query'\n" +
    "Removing unused arguments from blocks in resource, action and filter files\n" +
    "Updating resource and action files to use 'record' instead of 'model'\n" +
    "Updating resource files to use 'query' instead of 'model_class' and 'scope'\n" +
    "Renaming resource files to remove the '_resource' suffix\n" +
    "Removing the `config.tabs_style` from the avo initializer\n" +
    "\n" +
    "  - There are 2 ways of renaming the resource files:\n" +
    "    1. Using `git mv` command, that automaticly stage the changes and makes the commit review process easier.\n" +
    "    2. Using `mv` command, that will rename the files without relying on any specific version control system. You will have to stage the changes manually.\n"
  @mv_cmd = nil
  while @mv_cmd != "1" && @mv_cmd != "2"
    print "  Choose the one you prefer (1 or 2) and press enter: "
    @mv_cmd = gets.chomp
  end
  puts "---------------------\n" +
  "The following components will be upgraded:\n" +
  "\nResources: \n -#{resources_names.join("\n -")}\n"
  enter_to_continue
  puts "\nActions: \n -#{actions_names.join("\n -")}\n"
  enter_to_continue
  puts "\nFilters: \n -#{filters_names.join("\n -")}\n"
  enter_to_continue
  puts "\nResource tools: \n -#{resource_tools_names.join("\n -")}\n"
  enter_to_continue
  puts "\nDashboards: \n -#{dashboards_names.join("\n -")}\n"
  enter_to_continue
  puts "\nCards: \n -#{cards_names.join("\n -")}\n"
  enter_to_continue
  puts "\nThis upgrade will NOT:\n" +
    "- Apply the `def fields` and `def cards` API\n" +
    "- Remove the argument from lambda functions if they are not as we specify them on docs.\n" +
    "- Remove the index_text_align option\n" +
    "- Swap disabled with readonly\n\n"
end