Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:07:47+00:00 2026-05-17T21:07:47+00:00

Today I was faced with a challenge to create different behaviors for my shopping

  • 0

Today I was faced with a challenge to create different behaviors for my shopping cart model. That’s because the owner of the online shopping wanted to create some promotions like buy 1, get 5 or get 25% discount + some extra stuff, etc…

I thought of doing it with polymorphic inheritance, where my Cart model will only hold the products and accessories bought and will have many “Behavior Models” where it would get its data like the the total of the purchase (that could get 25% off) and call the methods like the one that closes the deal (where i could add new, specified products for the promotions where you get 5 free something).

In the beginning it seemed all right to me to create this kind of relationship as it wouldn’t require much work on the model interface as it would continue to communicate normally with other objects, just the internals would be changed. Now i’m seeing myself creating a lot of crap, repeated code over the behavior models and started figuring if there are other, more elegant ways, of doing this…

Do you know a better way?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:07:48+00:00Added an answer on May 17, 2026 at 9:07 pm

    I’ve solved this using a bit of metaprogramming! Now my cart model works like this:

      def after_initialize
    
        extend BoxStandardMethods
    
        if self.box_behavior_type?
          extend Object.const_get(box_behavior_type)
        end
      end
    

    where BoxStandardMethods is a module that has all the methods a standard cart has (add_product, remove_product, get_total, etc) and if i want to override any of these methods, i simply set the box_behavior_type attribute of the cart with a module that has the functions i want to override.
    To avoid incompatibility with the interfaces, i am building the extendable modules with custom fields that are backward-compatible, ie. the standard method add_product(product,amount) is implemented as add_product(product,amount,*custom_data) and if custom_data is nil, then the super method is called, effectively calling the method implemented on BoxStandardMethods

    Ruby is simply great for this kind of approach! The system is in production for about a week now and i’ve had no problems at all!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today we faced a quite simple problem that were made even simpler by the
Today I faced a problem that there is one memory leak in my iphone
I have a project that's been using font-face without problem for some time. Today
Today i faced a little problem, i wanted to know the reason of leak.
Today i am faced with a problem that the company i work for is
Today I faced a strange problem in C#. I have an ASP.NET page where
I faced an interesting problem today. I have 4 strings which I need to
Today, I have read that command's object in WPF can be serialized. And I'm
Today I faced one question in interview. Is it possible to apply inheritance concept
Today I was faced with the method constructServiceUrl() of the org.jasig.cas.client.util.CommonUtils class. I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.