price_log 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e494deb6d5a68cc4f62f1bed8379d86c8663eeab
4
- data.tar.gz: 17b1e1a2d09a1ea2f59863b79805c9a06ac27fc3
3
+ metadata.gz: 18f2b11a59e015ab970c40f8324c8b3c8b33d8bd
4
+ data.tar.gz: a7ae5addd80b445010888a7bbadac20bfe4c4eaa
5
5
  SHA512:
6
- metadata.gz: 39f91f596f3ad696434a10b474877b64c68816538fa93cf873d2a2a47a4b4e09a6be1b80eef25222ccb87872754fe33920c3939f899a297183d9beaee309ee9f
7
- data.tar.gz: 45bc5abcfdb315c4417b4277cd795e0421433e273293b808ce238b24247cb0524d79ab872fd912b8091a1c37056f9a0c484464ebfb209b2eee8218346cd649b5
6
+ metadata.gz: 8c4dc7ea2ffcc74a919672386c8e8e2aa19260ecaafe0bc9bd9aea9bd8071155ea38e78bf737c8ee0ade0e21cccab9d8c2d9d834f531325df24e373eacc5f4cf
7
+ data.tar.gz: 4888096a31a8936e5e514eb81c3e97cd2bc08f651678fad6a76044000bbda7eeb12ebedd41dc84f884ee64d41a59a6d863f94278deda07cd446e250d34404626
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andrzej Liśkiewicz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # PriceLog
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'price_log'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install price_log
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/price_log/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ class CreatePriceLogEntries < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :price_log_entries do |t|
4
+ t.money :price
5
+ t.datetime :start_date
6
+ t.datetime :end_date
7
+
8
+ t.references :priceable, :polymorphic => true
9
+ t.string :priceable_field_name
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :price_log_entries, :start_date
15
+ add_index :price_log_entries, :end_date
16
+ add_index :price_log_entries, :priceable_field_name
17
+ end
18
+
19
+ def self.down
20
+ drop_table :price_log_entries
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ class PriceLogEntry < ActiveRecord::Base
2
+ monetize :price_cents, with_model_currency: :price_currency
3
+ belongs_to :priceable, :polymorphic => true
4
+
5
+ after_create :resolve_dates
6
+
7
+ validates :priceable_id, :priceable_type, :priceable_field_name, presence: true
8
+
9
+ scope :all_for_priceable, ->(priceable_type, priceable_id, priceable_field_name){
10
+ where(priceable_id: priceable_id, priceable_type: priceable_type, priceable_field_name:priceable_field_name)
11
+ }
12
+ scope :for_date, ->(priceable_type, priceable_id, priceable_field_name, date) {
13
+ unless date.nil?
14
+ all_for_priceable(priceable_type, priceable_id, priceable_field_name).where('start_date <= ? AND (end_date IS NULL OR end_date > ?)', date, date )
15
+ else
16
+ all_for_priceable(priceable_type, priceable_id, priceable_field_name).where('start_date <= ? AND end_date IS NULL', DateTime.now)
17
+ end
18
+ }
19
+
20
+
21
+ def resolve_dates
22
+ _all_ = PriceLogEntry.all_for_priceable(self.priceable_type, self.priceable_id, self.priceable_field_name)
23
+ prev_start_date = nil
24
+ self.logger.info "Recalculates #{self.priceable_type}, #{self.priceable_id}, #{self.priceable_field_name}"
25
+ _all_.order("start_date DESC").each do |ple|
26
+ unless prev_start_date.nil?
27
+ ple.end_date = prev_start_date
28
+ ple.save!
29
+ end
30
+ prev_start_date = ple.start_date
31
+ end
32
+ true
33
+ end
34
+
35
+ def pretty_print
36
+ puts "#{self.priceable_type}, #{self.priceable_id}, #{self.priceable_field_name}, start_date: #{self.start_date}, end_date: #{self.end_date}"
37
+ end
38
+
39
+ end
@@ -0,0 +1,14 @@
1
+ module PriceLog
2
+
3
+ class Railtie < Rails::Railtie
4
+ generators do
5
+ require "generators/price_log/install_generator"
6
+ end
7
+ initializer 'price_log.insert_into_active_record' do
8
+ ActiveSupport.on_load :active_record do
9
+ ActiveRecord::Base.send(:include, PriceLog)
10
+ end
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,3 @@
1
+ module PriceLog
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ module PriceLog
2
+
3
+ module PriceLog
4
+ def self.included(price_log_model)
5
+ price_log_model.extend Finders
6
+ price_log_model.scope :in_order, -> { price_log_model.order('created_at ASC') }
7
+ price_log_model.scope :recent, -> { price_log_model.reorder('created_at DESC') }
8
+ end
9
+
10
+ module Finders
11
+
12
+ def find_price_log_for_priceable(priceable_type, priceable_id, priceable_field_name)
13
+ PriceLog.where( priceable_type: priceable_type, priceable_id: priceable_id, priceable_field_name: priceable_field_name )
14
+ end
15
+
16
+ def find_price_log_for_priceable_for_date(priceable_type, priceable_id, priceable_field_name, date)
17
+ PriceLog.find_price_log_for_priceable(priceable_type, priceable_id, priceable_field_name).where("start_date <= ? and ( end_date IS NULL or end_date <= ?)", date, date).first
18
+ end
19
+
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: price_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrzej Liśkiewicz
@@ -185,7 +185,16 @@ executables: []
185
185
  extensions: []
186
186
  extra_rdoc_files: []
187
187
  files:
188
+ - Gemfile
189
+ - LICENSE.txt
190
+ - README.md
191
+ - Rakefile
192
+ - lib/generators/price_log/templates/create_price_log_entries.rb
193
+ - lib/generators/price_log/templates/price_log_entry.rb
188
194
  - lib/price_log.rb
195
+ - lib/price_log/railtie.rb
196
+ - lib/price_log/version.rb
197
+ - lib/price_log_methods.rb
189
198
  homepage: ''
190
199
  licenses:
191
200
  - MIT