Class: SpreeDelhivery::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/spree_delhivery/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_migrationsObject



8
9
10
# File 'lib/generators/spree_delhivery/install/install_generator.rb', line 8

def add_migrations
  run 'bundle exec rake railties:install:migrations FROM=spree_delhivery'
end

#configure_cms_blocksObject



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
# File 'lib/generators/spree_delhivery/install/install_generator.rb', line 54

def configure_cms_blocks
  # We don't ask for permission here, we just do it if the table exists, 
  # as it's a non-destructive UI enhancement.
  
  unless Spree::PageSection.table_exists?
    say_status :skipped, "Spree::PageSection table not found. Skipping CMS config.", :yellow
    return
  end

  say_status :configuring, "Checking Product Page Sections for Delhivery Widget..."
  
  count = 0
  Spree::PageSection.where(type: "Spree::PageSections::ProductDetails").find_each do |section|
    next if section.blocks.where(type: "Spree::PageBlocks::Products::DelhiveryEdd").exists?

    section.blocks.create!(
      type: "Spree::PageBlocks::Products::DelhiveryEdd",
      position: (section.blocks.maximum(:position) || 0).to_i + 1
    )
    count += 1
    say_status :added, "Delhivery Widget to '#{section.name}'", :green
  end
  
  if count > 0
    say_status :complete, "Added Delhivery Widget to #{count} sections.", :green
  else
    say_status :complete, "CMS Blocks already configured.", :blue
  end
rescue StandardError => e
  say_status :error, "Could not configure CMS blocks: #{e.message}", :red
end

#run_migrationsObject



12
13
14
15
16
17
18
19
# File 'lib/generators/spree_delhivery/install/install_generator.rb', line 12

def run_migrations
  run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
  if run_migrations
    run 'bundle exec rake db:migrate'
  else
    say_status :skipped, "Skipping rails db:migrate, don't forget to run it!", :yellow
  end
end

#seed_shipping_methodsObject



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
# File 'lib/generators/spree_delhivery/install/install_generator.rb', line 21

def seed_shipping_methods
  unless run_seeding?("Delhivery Surface and Express shipping methods")
    say_status :skipped, "Skipping Shipping Method creation.", :yellow
    return
  end

  # Safety Check: Ensure tables exist (in case user skipped migrations)
  unless Spree::ShippingMethod.table_exists? && Spree::Calculator.table_exists?
    say_status :error, "Database tables missing. Run migrations first.", :red
    return
  end

  # Ensure Calculator class is available
  unless calculator_class_available?
    say_status :error, "Spree::Calculator::Shipping::Delhivery not found. Restart server/console.", :red
    return
  end

  # Locate dependencies (Zones/Categories)
  shipping_category = Spree::ShippingCategory.find_by(name: 'Default') || Spree::ShippingCategory.first
  shipping_zone     = Spree::Zone.find_by(name: 'India') || Spree::Zone.first

  if shipping_category.nil? || shipping_zone.nil?
    say_status :warning, "Could not find a Shipping Category or Zone. Created methods will need manual setup.", :yellow
  end

  # Create Methods using Helper
  create_delhivery_method("Delhivery Surface", "DELHIVERY_S", "Surface", shipping_category, shipping_zone)
  create_delhivery_method("Delhivery Express", "DELHIVERY_E", "Express", shipping_category, shipping_zone)
rescue StandardError => e
  say_status :error, "Could not seed shipping methods: #{e.message}", :red
end