rolemodel_sower 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a83f928ff615025f32fe7060888fbf45508825a44593de8752447324d9c3b53
4
- data.tar.gz: cd767fdd458dfd25c62a87a247068d467e3b13bf4cfbbb612b8e6fd4f96356b6
3
+ metadata.gz: afa9c43c72ba7bef3fda89928296f16c18fedda9829b56b49aea9da7c8446e81
4
+ data.tar.gz: d2d0b1075dff10a91b0a27ca9409eeb58ae9fc7c32b59218ff8f0e83323babe0
5
5
  SHA512:
6
- metadata.gz: 701c5bfc6fab930f3c0038420e3b24c8828a6677512af4314cea13dac80d808ea798773036c336911419526ea0c1c54ea9a505467ad88947dae2844278d2ef64
7
- data.tar.gz: 26aef1405e5a63fdf383f5ea257de0e7ba9c218f70ec5b4e3f686ddecb8c982dbc264b55e9d57f9208e5fed090325232180dca65fc3acf2963b7488a4a8587e1
6
+ metadata.gz: 52cf43b5235e4bb8a61ccc31fc7b18e41b4b1e71dd9c43582021a295ea52a5cb5ebd7bb5f1828c2345fa53132d38da362ef0672e0015c2bfa7454c57e2fc2d35
7
+ data.tar.gz: 4ae6ce8841263815b315ff3b57550f9a95af7e878c7011de407662daa9c41c8432c7de004c11399709f5b2befe71b3038340db68c30b457371e2d6b2c14bfcfa
data/README.md CHANGED
@@ -30,9 +30,17 @@ Seed data is created by placing files in the `db/rolemodel_sower_data` directory
30
30
 
31
31
  `db/rolemodel_sower_data/users.json`
32
32
 
33
+ The seed data directory can be configured in the initializer generated by the install generator.
34
+
35
+ ```ruby
36
+ RolemodelSower.setup do |config|
37
+ config.data_path = 'db/seed_data'
38
+ end
39
+ ```
40
+
33
41
  ### Translating Seed Data
34
42
 
35
- You will need to define a rolemodel_sower file to take the data parsed from your seed data and turn create the records in your database.
43
+ You will need to define a rolemodel_sower file to take the data parsed from your seed data and create the records in your database.
36
44
 
37
45
  Sower files should be created in `app/seeds/rolemodel_sower`. The files should be named after the model they are creating data for. For example, a file named `user.rb` would create seed data for the `User` model.
38
46
 
@@ -57,12 +65,12 @@ In `app/seeds/rolemodel_sower/user.rb`
57
65
  # frozen_string_literal: true
58
66
 
59
67
  module RolemodelSower
60
- class User < Sower::Base
68
+ class User < RolemodelSower::Base
61
69
  def load!
62
70
  ::User.find_or_create_by!(
63
- first_name: @data[:first_name],
64
- last_name: @data[:last_name],
65
- email: @data[:last_name]
71
+ first_name: data[:first_name],
72
+ last_name: data[:last_name],
73
+ email: data[:last_name]
66
74
  )
67
75
  end
68
76
  end
@@ -73,9 +81,23 @@ end
73
81
 
74
82
  Seeds can be generated by calling the `seed!` method on `RolemodelSower`.
75
83
 
76
- It takes one or more symbol arguments that correspond to sets of seed data and runs then in the order passed.
84
+ It takes one or more symbol arguments that correspond to sets of seed data and runs them in the order passed.
85
+
86
+ ```ruby
87
+ RolemodelSower.seed!(
88
+ :users,
89
+ :organizations,
90
+ :facilities
91
+ )
92
+ ```
93
+
94
+ If you want to add conditions to each run, you can separate out each call to seed.
77
95
 
78
- `RolemodelSower.seed!(:users, :organizations, :facilities)`
96
+ ```ruby
97
+ RolemodelSower.seed!(:users) if my_condition
98
+ RolemodelSower.seed!(:organizations) if other_condition
99
+ RolemodelSower.seed!(:facilities) if another_condition
100
+ ```
79
101
 
80
102
  ## Adapters
81
103
 
@@ -86,3 +108,41 @@ RolemodelSower.setup do |config|
86
108
  config.adapter = :json
87
109
  end
88
110
  ```
111
+
112
+ You can also specify the adapter when calling `seed!`. This will override the default adapter or what is set in the initializer.
113
+
114
+ ```ruby
115
+ RolemodelSower.seed!(
116
+ { users: :csv },
117
+ :organizations,
118
+ { facilities: :json }
119
+ )
120
+ ```
121
+
122
+ ```ruby
123
+ RolemodelSower.seed!({ users: :csv }) if my_condition
124
+ RolemodelSower.seed!(:organizations) if other_condition
125
+ RolemodelSower.seed!({ facilities: :json }) if another_condition
126
+ ```
127
+
128
+ ## Development
129
+
130
+ To build the gem locally, run: `gem build rolemodel_sower.gemspec`
131
+
132
+ To test the gem locally, use the path option in your Gemfile:
133
+
134
+ ```ruby
135
+ gem 'rolemodel_sower', path: '../rolemodel_sower'
136
+ ```
137
+
138
+ ## Publishing
139
+
140
+ Make sure you are an owner of the gem.
141
+
142
+ You also need to be signed in to rubygmes by running:
143
+
144
+ `gem signin`
145
+
146
+ To publish, run:
147
+
148
+ `gem push rolemodel_sower-{major.minor.patch}.gem`
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Sower
4
- class Organization < Sower::Base
3
+ module RolemodelSower
4
+ class Organization < RolemodelSower::Base
5
5
  def load!
6
- ::Organization.find_or_create_by! name: @data[:name]
6
+ ::Organization.find_or_create_by! name: data[:name]
7
7
  end
8
8
  end
9
9
  end
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Sower
4
- class User < Sower::Base
3
+ module RolemodelSower
4
+ class User < RolemodelSower::Base
5
5
  def load!
6
6
  user = ::User.create_with(
7
- first_name: @data[:first_name],
8
- last_name: @data[:last_name],
9
- password: @data[:password],
10
- password_confirmation: @data[:password],
7
+ first_name: data[:first_name],
8
+ last_name: data[:last_name],
9
+ password: data[:password],
10
+ password_confirmation: data[:password],
11
11
  organization: ::Organization.first,
12
- ).find_or_create_by! email: @data[:email]
12
+ ).find_or_create_by! email: data[:email]
13
13
 
14
14
  # Any models can be affected here.
15
15
  ::Profile.find_or_create_by!(user: user)
@@ -1,3 +1,3 @@
1
1
  To copy a Sower initializer to your Rails App, with some configuration values, just do:
2
2
 
3
- rails generate sower:install
3
+ rails generate rolemodel_sower:install
@@ -3,4 +3,7 @@
3
3
  RolemodelSower.setup do |config|
4
4
  # Available Adapters :yaml, :csv, :tsv, :json
5
5
  # config.adapter = :yaml
6
+
7
+ # Path to the directory containing the seed data files
8
+ # config.data_path = 'db/rolemodel_sower_data'
6
9
  end
@@ -12,7 +12,7 @@ module RolemodelSower
12
12
  end
13
13
 
14
14
  def self.path(seed_name)
15
- Rails.root.join("db/rolemodel_sower_data/#{seed_name.to_s.pluralize}#{file_extension}")
15
+ Rails.root.join("#{RolemodelSower.data_path}/#{seed_name.to_s.pluralize}#{file_extension}")
16
16
  end
17
17
 
18
18
  def self.class_name(seed_name)
@@ -2,6 +2,8 @@
2
2
 
3
3
  module RolemodelSower
4
4
  class Base
5
+ attr_reader :data
6
+
5
7
  def initialize(data)
6
8
  @data = data
7
9
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RolemodelSower
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -14,6 +14,9 @@ module RolemodelSower
14
14
  mattr_accessor :adapter
15
15
  @@adapter = :yaml
16
16
 
17
+ mattr_accessor :data_path
18
+ @@data_path = 'db/rolemodel_sower_data'
19
+
17
20
  def self.setup
18
21
  yield self
19
22
  end
@@ -22,10 +25,15 @@ module RolemodelSower
22
25
 
23
26
  def self.seed!(*seed_names)
24
27
  seed_names.each do |seed_name|
28
+ current_seed_name = seed_name.try(:keys)&.first || seed_name
29
+ current_adapter = seed_name.try(:values)&.first || adapter
30
+ pluralized_name = current_seed_name.to_s.classify
31
+
25
32
  logger = Logger.new($stdout)
26
- pluralized_name = seed_name.to_s.classify
27
33
  logger.info "Seeding #{pluralized_name}..." if Rails.env.development?
28
- "RolemodelSower::Adapters::#{adapter.to_s.upcase}".constantize.all(seed_name).each(&:load!)
34
+
35
+ "RolemodelSower::Adapters::#{current_adapter.to_s.upcase}".constantize.all(current_seed_name).each(&:load!)
36
+
29
37
  logger.info "Seeding #{pluralized_name}... done" if Rails.env.development?
30
38
  end
31
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolemodel_sower
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
  - Jeremy Walton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-19 00:00:00.000000000 Z
11
+ date: 2023-08-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby gem to simplify Seed data creation for Rails applications.
14
14
  email: consult@rolemodelsoftware.com