Giter Site home page Giter Site logo

Comments (4)

kjvarga avatar kjvarga commented on September 17, 2024

Hi there,

I'm glad you enjoy the gem :) Your problem here is easily solved by using named variables rather than instance variables. Because @website_id looks for the website_id on the current instance (context) which changes as you have seen from your output. So just replace @website_id with website_id, @website with website etc.

from sitemap_generator.

arufanov avatar arufanov commented on September 17, 2024

-1.Thank you for answer, this is working for me, here is code:
def export_sitemap
# Init variables
website = @website
website_id = @website_id

# Create sitemap files
SitemapGenerator::Sitemap.default_host = @website.domain
SitemapGenerator::Sitemap.public_path = "tmp/"
SitemapGenerator::Sitemap.sitemaps_path = "sitemaps/#{@website.code}"
SitemapGenerator::Sitemap.adapter = SitemapGenerator::WaveAdapter.new
SitemapGenerator::Sitemap.create do |interpreter|
  puts 'test1'  + @website_id.inspect
  puts 'test2'  + website_id.inspect

  StaticContent.find(:all, :conditions => ["website_id = ?", website_id]).each do |item|
    interpreter.add item.url
  end
end

end

output
test1nil
test21
test1nil
test22
test1nil
test23

I would be great add this use case to documenation, it would be save me about 6 hours.

-2.Also this problem has other solution: if def self.yield_sitemap? return true, then interpreter.eval(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block) get true and then interpretator eval is executed on the right context (context where @website is defined) and I should not create a named variable.
def eval(opts={}, &block)
if block_given?
if opts[:yield_sitemap]
yield self
else
instance_eval(&block)
end
end
end

I have created an initializer with code (monkey patching):
module SitemapGenerator

Returns true if we should yield the sitemap instance to the block, false otherwise.

def self.yield_sitemap?
# Retrun true always
#!!@yeild_sitemap
true
end
end

and solution is working fine for me. I am not able to unerstand how can I setup @yeild_sitemap to true without monkey patching. I will appretiate the answer and it would be greate to add a little note to documentation.

Meantime the problem is resolved and I am ready to close this issue. Thank you very much for answer and for gem!

Sincerely yours,
Rufanov Artem.

P.S.
Have a good day!

from sitemap_generator.

kjvarga avatar kjvarga commented on September 17, 2024

Actually there is already documentation in the README. Take a look at the Generating Multiple Sitemaps section.

I don't understand why you are using the :yield_sitemap option? You don't need to use that option. It's just there for backwards compatibility with the old version which passed the sitemap to the block. Just use the example I pasted in.

So your create call should look like this:

SitemapGenerator::Sitemap.create do
  StaticContent.find(:all, :conditions => ["website_id = ?", website_id]).each do |item|
    add item.url # you should be adding a path
  end
end

Also you should be passing a path in your call to add(). The full url is built from the default_host and the path.

BTW it's very simple to set yield_sitemap, but you don't need to use it: SitemapGenerator.yield_sitemap = true

from sitemap_generator.

arufanov avatar arufanov commented on September 17, 2024

Thank you very much!

from sitemap_generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.