Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ And then execute:
$ bundle
```

Or install it yourself as:
Or install it with bundle install:
```bash
$ gem install submane
$ bundle install submane
```

We then run the following command to add submane config file to your initializers
We then run the following command to add migrations and config yml file
```bash
$ bin/rails generate submane:config
$ bin/rails generate submane:install
```

Finally, we can ser the class that subscriptions will be attached to.
Assuming "Account" class will hold the subscriptions, then we set is as the following string,
Finally, we can add the class that subscriptions will be attached to.
Assuming "Account" class will hold the subscriptions, then we set is as the following string in `config/submane.yml`
```ruby

Submane.configuration do |config|
config.account_class_name = "Account"
end

account_class: Account
```

## Usage
Expand Down
33 changes: 13 additions & 20 deletions app/models/concerns/submane/model_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
module Submane
module ModelAttributes
extend ActiveSupport::Concern
def create_subscription; end

included do
def create_subscription; end

def active_subcriptions
subscriptions.where("end_date > ?", Time.now).order(:end_date)
end

def active_plan
subscriptions.where("end_date > ?", Time.now).order(:end_date).last.submane_plan
end

def active_plan?
subscriptions.where("end_date > ?", Time.now).exists?
end
def active_subcriptions
subscriptions.where("end_date > ?", Time.now).order(:end_date)
end

def active_subscription
subscriptions.where("end_date > ?", Time.now).order(:end_date).last
end
def active_plan
subscriptions.where("end_date > ?", Time.now).order(:end_date).last.submane_plan
end

alias_method :subcribed?, :active_plan?
def active_plan?
subscriptions.where("end_date > ?", Time.now).exists?
end

class_methods do
def active_subscription
subscriptions.where("end_date > ?", Time.now).order(:end_date).last
end

alias subcribed? active_plan?
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/submane/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Models

class_methods do
def setup_submane
has_many :subscriptions, class_name: "Submane::Subscription"
has_many :subscriptions, class_name: "Submane::Subscription", foreign_key: "client_id"
include Submane::ModelAttributes
end
end
Expand Down
1 change: 0 additions & 1 deletion app/models/submane/plan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Submane
class Plan < ApplicationRecord
self.table_name = "submane_plans"
has_many :subscriptions
validates :name, :price, :visual_order, presence: true
end
Expand Down
5 changes: 2 additions & 3 deletions app/models/submane/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Submane
class Subscription < ApplicationRecord
self.table_name = "submane_subscriptions"
belongs_to :submane_plan, class_name: "Plan"
belongs_to Submane.account_class_name_sym, class_name: Submane.account_class_name
belongs_to :plan
belongs_to :client, class_name: Submane.account_class if Submane.account_class
end
end
11 changes: 0 additions & 11 deletions db/migrate/20240623154709_create_plan.rb

This file was deleted.

11 changes: 0 additions & 11 deletions db/migrate/20240623161156_create_subscription.rb

This file was deleted.

9 changes: 9 additions & 0 deletions lib/generators/submane/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Creates migration and initializer config files for Submane

Example:
bin/rails generate submane:install

This will create:
config/initializers/submane.rb
db/migrate/{timestamp}_install_submane.rb
8 changes: 0 additions & 8 deletions lib/generators/submane/config/USAGE

This file was deleted.

9 changes: 0 additions & 9 deletions lib/generators/submane/config/config_generator.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/generators/submane/config/templates/submane_config.rb.tt

This file was deleted.

24 changes: 24 additions & 0 deletions lib/generators/submane/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "rails/generators/active_record"

module Submane
module Generators
class InstallGenerator < Rails::Generators::Base
include ActiveRecord::Generators::Migration
source_root File.expand_path("templates", __dir__)

def copy_migration
migration_template "migration.rb", "db/migrate/install_submane.rb", migration_version: migration_version
end

def copy_config
template "config.yml", "config/submane.yml"
end

private

def migration_version
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
end
end
end
end
1 change: 1 addition & 0 deletions lib/generators/submane/templates/config.yml.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
account_class: User
19 changes: 19 additions & 0 deletions lib/generators/submane/templates/migration.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table :submane_plans do |t|
t.string :name
t.integer :price
t.integer :visual_order, index: { unique: true }

t.timestamps
end

create_table :submane_subscriptions do |t|
t.references :plan
t.references :client
t.datetime :end_date, null: false

t.timestamps
end
end
end
24 changes: 14 additions & 10 deletions lib/submane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
require "submane/attributes"

module Submane
include ActiveSupport::Configurable
extend Attributes

config_accessor :account_class_name
# config_accessor :account_class

class << self
def configuration(&block)
return unless const_defined? Rails.to_s

Rails.configuration.before_initialize do
block.call config
def self.config
@config ||= begin
path = Rails.root.join("config", "submane.yml").to_s
if File.exist?(path)
YAML.safe_load(ERB.new(File.read(path)).result, aliases: true)
else
{}
end
end
end

def account_class_name_sym
account_class_name.to_s.split("::").last.underscore.to_sym
def self.account_class
unless defined?(@account_class)
@account_class = config["account_class"]
@account_class = (Account.name rescue nil) if @account_class.nil? # rubocop:disable Style/RescueModifier
end
@account_class
end
end

Expand Down
3 changes: 0 additions & 3 deletions test/dummy/config/initializers/submane.rb

This file was deleted.

1 change: 1 addition & 0 deletions test/dummy/config/submane.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
account_class: User
19 changes: 19 additions & 0 deletions test/dummy/db/migrate/20250125140439_install_submane.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class InstallSubmane < ActiveRecord::Migration[7.1]
def change
create_table :submane_plans do |t|
t.string :name
t.integer :price
t.integer :visual_order, index: { unique: true }

t.timestamps
end

create_table :submane_subscriptions do |t|
t.references :plan
t.references :client
t.datetime :end_date, null: false

t.timestamps
end
end
end
12 changes: 5 additions & 7 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_06_24_123342) do
ActiveRecord::Schema[7.1].define(version: 2025_01_25_140439) do
create_table "submane_plans", force: :cascade do |t|
t.string "name"
t.integer "price"
Expand All @@ -21,13 +21,13 @@
end

create_table "submane_subscriptions", force: :cascade do |t|
t.integer "submane_plan_id", null: false
t.integer "user_id", null: false
t.integer "plan_id"
t.integer "client_id"
t.datetime "end_date", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["submane_plan_id"], name: "index_submane_subscriptions_on_submane_plan_id"
t.index ["user_id"], name: "index_submane_subscriptions_on_user_id"
t.index ["client_id"], name: "index_submane_subscriptions_on_client_id"
t.index ["plan_id"], name: "index_submane_subscriptions_on_plan_id"
end

create_table "users", force: :cascade do |t|
Expand All @@ -37,6 +37,4 @@
t.datetime "updated_at", null: false
end

add_foreign_key "submane_subscriptions", "submane_plans"
add_foreign_key "submane_subscriptions", "users"
end
9 changes: 9 additions & 0 deletions test/fixtures/submane_plans.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
basic:
name: basic
price: 50
visual_order: 1

advance:
name: advance
price: 100
visual_order: 2
4 changes: 4 additions & 0 deletions test/fixtures/submane_subscriptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
david_subscription:
client: david
plan: basic
end_date: <%= Time.new + 3.months %>
3 changes: 3 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
david:
name: David Costa
description: Another man alive
15 changes: 0 additions & 15 deletions test/lib/generators/submane/config/config_generator_test.rb

This file was deleted.

25 changes: 25 additions & 0 deletions test/lib/generators/submane/install_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helper"
require_relative "../../../../lib/generators/submane/install_generator"

module Submane
class InstallGeneratorTest < Rails::Generators::TestCase
tests Generators::InstallGenerator
destination File.expand_path("../tmp", File.dirname(__FILE__))
setup :prepare_destination

test "generator creates config initializer" do
run_generator ["submane:install"]
assert_file "config/submane.yml"
end

test "generator creates migration_file" do
run_generator ["submane:install"]
assert_migration "db/migrate/install_submane.rb" do |migration|
assert_instance_method :change, migration do |change|
assert_match(/create_table :submane_plans/, change)
assert_match(/create_table :submane_subscriptions/, change)
end
end
end
end
end
1 change: 1 addition & 0 deletions test/lib/generators/tmp/config/submane.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
account_class: User
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class InstallSubmane < ActiveRecord::Migration[7.1]
def change # rubocop:disable Metrics/MethodLength
create_table :submane_plans do |t|
t.string :name
t.integer :price
t.integer :visual_order, index: { unique: true }

t.timestamps
end

create_table :submane_subscriptions do |t|
t.references :plan
t.references :client
t.datetime :end_date, null: false

t.timestamps
end
end
end
Loading
Loading