Discounter
Discounter allows you to create a tree of discount tiers for your application (currently max'd at 3, dynamic max coming soon). We allow you to use this tree on the front-end via form post helpers or on the backend to help track and calculate currently applied discounts for a user. This is aiming to be an entirely end-to-end solution for discounting.
Installation
Add this line to your application's Gemfile:
gem 'discounter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install discounter
Usage
After you have required the gem in your project, then you can initialize it and add your discounts.
Note: you will not need to initialize both modules to work with this gem, you can pick if you just want it to help you with your views or with your calculations.
First, add your discounts to the tree:
@discounts = Discounts.new(discount_percentage_1, discount_percentage_2, discount_percentage_3)
Then you have access to the following methods:
@discounts.all
@discounts.select_helper
Now, let's say you have used the select helper to post a discount to the server in a join record.
Let's also pretend the entity is called Event, the join is called EventUser, and the user is simply a User record.
In that case we might have a controller that looks like this:
@event = Event.find(params[:id])
@user = current_user
With that, we can easily calculate our new event price if the user viewing it has a discount able to be applied to them in the system. Keep in mind we need to pass in the join model last.
@discounter = Discounter.new(@event)
@event.price = @discounter.evaluate_discount(@user, EventUser)
Then we will query the database in an optimized fashion and find the discount able to be applied to that user, and if so we will go ahead and manipulate the current @event.price and return out the new discounted price.
Then using @event in your view will easily allow you to display each current_users customized price.
ToDo:
- Add dynamic max, allowing users to mix in as many discount tiers as they want for their application.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request